Cmdlet Structure & Help.md - Juan-bit94/Ops401D10 GitHub Wiki

Cmdlet Structure & Help

What is a cmdlet?

  • Its a lightweight command that performs a single function
  • Used throughout the Windows PowerShell environment.
  • Runtime invokes cmdlets depending on the automation scripts used at the command line.
    • Can be invoked by Windows PowerShell API
  • Has more than 200 basic core cmdlets available by Windows PowerShell
  • is a .ps1 extension
  • Each cmdlet has a help file that provides details such as:
    • Description, including description of parameters
    • Syntax
    • Example of use:
      • Cmdlet format is straightforward using a verb-prefix_singular_noun method. And they usually perform an action and return a .NET object to next command.
      • They implement a class that is retrieved from one of the cmdlet base classes. And cmdlet operations declare an attribute that identifies derived class, defines public properties associated with the derived class, and override one or more processing methods in order to execute results.

How do Cmdlets differ from commands?

  • Cmdlets are instances of .NET classes and are not considered as a standalone executables.
  • Can be created with less than a dozen lines of code.
  • Process input object from pipeline.

Commonly used cmdlets

  • Here is a list of popular cmdlets
  • Copy-Item : Copies file(s)
  • Get-Location : Gets the directory that the user is in.
  • Move-Item : Moves file(s)
  • New-Item : Creates a new file or directory
  • Remove-Item : Deletes a file or directory
  • Rename-Item : Renames file(s)
  • Set-Location : Changes directory
  • When you start working with PowerShell, you will find yourself getting more familiar with these to start with.
  • Example: We will use the Set-Location and this simply changes directory in PowerShell.
    • PS C:> Set-Location ".\Documents and Settings"

PowerShell Command History

  • There's a cmdlet called get-history. If you type it and press enter, you will get a list of various commands that the user has typed in the session.
  • The information is saved in the buffer. Now there is a buffer size, and when that buffer gets maxed out, what will happen is every new command will push out the one on the bottom of the heap. So the oldest command will disappear.
  • To set the buffer size, click on the PowerShell icon on the top left, and go to properties. You will go to the options tab, then there is a command history section with options such as buffer size and number of buffers that you can adjust.

The Help System

  • The PowerShell has an extremely robust help system. You can find anything that you need to know in there. And it can be a bit overwhelming at first because the problem with a system like PowerShell is there's so much information to sift through.
  • To get started, just type help. Now, this is sort of intuitive thing but the first thing that people tend to do if they're dealing with an interface like the PowerShell, they don't know what to type.
  • Help, and this will actually give you information on the PowerShell help system itself. Its actually the Get-Help command. After running the command, hit the space bar to go down a page.
  • The way to use the get-help command is as so:
    • Example:
      • get-help *[cmdlet]*
      • get-help *set-acl*
  • If we enter the example above, we get a nice short response showing us the name of the cmdlet, the syntax, now there's no information here on the syntax whether there are any aliases for this, or whether there are any remarks.
  • Lets try the something else, something we know; get-help *set-location*
    • Running the command will inform us about the cmdlet, its syntax, and you can see there are three examples there, its aliases. This cmdlet has three aliases such as sl, cd, and chdir. The use of the asterisk is a wild card, it searches anything that is inside the asterisks.
  • You can also use the get-help -category switch to show just aliases that are in PowerShell. Can be used to narrow it down by different categories like function or cmdlets.

Updating Help

  • This section covers how to update the PowerShell help system. Like any other good software, it is good to be updated or be subject to updates. Its often a good idea to make sure that you are using the most recent version of the help system.
  • To look up how to update, just type get-help. You will see in the examples section that there is a command called Update-Help, running this command will update the system.

Exercise: Use PowerShell

  • Now that you've learned the fundamentals of PowerShell, let's put some of that knowledge to work. In this exercise you will perform basic PowerShell operations.
  1. You'll check your version of PowerShell
    • $psversiontable.psversion
  2. Use the PowerShell cmdlet that lists you directory location.
    • Set-Location
  3. Use a DOS or Linux command in PowerShell to list directory contents.
    • Get-ChildItem
  4. Use a PowerShell cmdlet to create a file. -** New-Item myitem.txt**
  5. Use the set-location command in PowerShell ISE to change the directory location.
    • Set-Location C:\new