Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Go pro: The power user's guide to PowerShell

Woody Leonhard | Oct. 4, 2016
PowerShell is a powerful tool to master. Here’s our step-by-step guide to getting familiar with Windows’ über language

To get all the details about parameters for the get-childitem cmdlet, or any other cmdlet, use the -full parameter, like this:

get-help get-childitem -full

That produces a line-by-line listing of what you can do with the cmdlet and what may (or may not!) happen. See the screenshot.

PowerShell get-help -full

Sifting through the parameter details, it’s reasonably easy to see that get-childitem can be used to retrieve “child” items (such as the names of subfolders or filenames) in a location that you specify, with or without specific character matches. For example:

get-childItem "*.txt" -recurse

retrieves a list of all of the “*.txt” files in the current folder and all subfolders (due to the -recurse parameter). Whereas the following:

get-childitem “HKLM:\Software”

returns a list of all of the high-level registry keys in HKEY_LOCAL_MACHINE\Software.

If you’ve ever tried to get inside the registry using a Windows command line or a batch file, I’m sure you can see how powerful this kind of access must be.

Step 5: Nail down the names

There’s a reason why the cmdlets we’ve seen so far look the same: get-childitem, update-help, and get-help all follow the same verb-noun convention. Mercifully, all of PowerShell’s cmdlets use this convention, with a verb preceding a (singular) noun. Those of you who spent weeks struggling over inconsistently named VB and VBA commands can breathe a sigh of relief.

To see where we’re going, take a look at some of the most common cmdlets (thanks to Ed Wilson’s Hey, Scripting Guy! blog). Start with the cmdlets that reach into your system and pull out useful information, like the following:

  • set-location: Sets the current working location to a specified location
  • get-content: Gets the contents of a file
  • get-item: Gets files and folders
  • copy-item: Copies an item from one location to another
  • remove-item: Deletes files and folders
  • get-process: Gets the processes that are running on a local or remote computer
  • get-service: Gets the services running on a local or remote computer
  • invoke-webrequest: Gets content from a web page on the internet

To see how a particular cmdlet works, use get-help, as in

get-help copy-item -full

Based on its help description, you can readily figure out what the cmdlet wants. For example, if you want to copy all your files and folders from Documents to c:\temp, you would use:

copy-item c:\users\[username] \documents\* c:\temp

As you type in that command, you’ll see a few nice touches built into the PowerShell environment. For example, if you type copy-i and press the Tab key, PowerShell fills in Copy-Item and a space. If you mistype a cmdlet and PowerShell can’t figure it out, you get a very thorough description of what went wrong.


Previous Page  1  2  3  4  5  6  7  Next Page 

Sign up for CIO Asia eNewsletters.