April 14th, 2012 Leave a comment Go to comments

PowerShell is the premier scripting language on Windows (installed by default in Vista and newer, downloadable for XP). PowerShell is targeted at administrators but is fully useful for both administrators and developers alike. PowerShell brings the full force of .NET to the command line with significant improvements in command line functionality over that of the older CMD window and even over shells like bash.

PowerShell deals directly in objects. Instead of having to parse strings to extract structure from the output of a tool in the pipeline, you have access to the actual object that the command returns. This means you can examine and filter on properties and call methods on that object. It helps to have .NET experience when using PowerShell but it is not an absolute requirement. You can learn and benefit from PowerShell without having to first make an investment of time in .NET and you’ll still derive great benefit from it.

There are key concepts to keep in mind when using PowerShell. Mastering these key concepts will greatly enhance your productivity.

  • Built in commands (CmdLets), functions, and CmdLets you write always return objects and not simple string values.
  • Objects can be passed into commands using the pipeline. The pipe is conceptually the same as compared to old fashioned CMD but it’s not strings but objects that are being passed between commands
  • PowerShell has access to the full suite of .NET objects which can be instantiated right on the command line and stored in variables. For example if you want to you can use Windows Forms .NET objects to create and display GUI’s using a script file
  • Errors are not passed back on stderr but instead are placed in a stack in a variable called $error . The latest error is always at $error[0] and the count of errors is $error.count . You can clear the error stack by calling $error.clear()
  • PowerShell supports introspection using a CmdLet called Get-Member . This is aliased as gm. You can always explore the structure of an object by passing it to Get-Member. For example
    dir | gm

    dir is an alias for Get-ChildItem which gets the items (usually files and directories) in the current directory. This comes back as an array of item objects and passing that to gm will get the members (properties and methods) of an item object (not of array).

  • PowerShell supports a rich built in help system. Type Get-Help <CmdLet name here> to get help. Use the -full argument to get the full help on a subject. To find what help is available for a particular subject use the wildcard. For example
    Get-Help Get-*

    This will return a list of all help for CmdLets starting with Get-

  • PowerShell uses the verb-noun convention for CmdLet names . For example
    Get-Process, Get-Item, Set-ExecutionPolicy
  • PowerShell has script execution disabled by default as a security precaution. On first use the administrator must enable local script execution. The safest compromise is RemoteSigned . Open an administrator powershell prompt and type
    Set-ExecutionPolicy RemoteSigned

    See help about_Execution_Policies for more details.

  • CmdLets usually take their arguments as named arguments. For example
    Get-Process -ComputerName <computer name here> -FileVersionInfo

    This shows the usage of two types of parameters. The first takes an argument, in this case the name of the computer you want the process list from. The second parameter is a switch parameter that takes no arguments.

  • PowerShell supports tab completion. Type the beginning part of your CmdLet or function name then tap the tab key until you get the result you want. Tab completion also works for parameter names. Type the dash (-) and tap the tab key until you get the argument you want.
  • PowerShell fully supports debugging. You can use the built in Integrated Scripting Environment or a free tool like PowerGUI . Both allow you to set breakpoints, inspect variable values, and edit those values while single stepping through your script.

Microsoft has written a superb getting started guide in the help file for the PowerShell integrated scripting environment. In the search bar in the start menu, search for PowerShell. Click on the Windows Powershell ISE and view the help file. This is the perfect place to start your journey with PowerShell. In the pages that folllow I will cover useful tips and tricks that have helped make my scripting in PowerShell more productive and will hopefully help you too.

  1. No comments yet.
  1. No trackbacks yet.