How to automate Windows with PowerShell

Faster editing

If you need to reuse a PowerShell command, don't waste time retyping it. Press [F7] to view your recent history, select a command, press [Enter] to run it, or the right arrow to place it on the command line. See http://tinyurl.com/qyovam2 for more editing shortcuts.

Use comments

Adding comments to your PowerShell scripts will help others understand what they do (or act as a reminder for you). Type a # character and whatever you enter for the rest of that line will be treated as a plain text comment and not processed as code.

Access Control Panel

PowerShell can access Windows components, as well as files. Use Get-ControlPanelItem to get a list of Control Panel applets, and something like Show-ControlPanelItem *display* to launch one.

Record a PowerShell session

Reviewing what happened in a PowerShell session can help the learning process. Enter the cmdlet Start-Transcript and all your console activities (including any output) are saved to a text file in your Documents folder. Enter Stop-Transcript to stop recording.

Scripted downloads

Need to download a file from a script? This is the easy way (see http://tinyurl.com/pdpvhdo for something smarter):

$client = New-Object "System.Net.WebClient"

$client.DownloadFile("http://domain.com/bigfile.zip","c:\temp\bigfile.zip"

Send email

You don't have time to monitor everything your scripts are doing? You could set them up to send you an email, instead. Check the Send-MailMessage cmdlet for a basic example, or Peter Morrisey's blog for something more powerful.

Get examples

If you're not sure how to use a cmdlet, use the Get-Help -examples switch, such as Get-Help Invoke-WebRequest -examples, to learn more.

RSS reader

The Invoke-RestMethod cmdlet can access the RSS feed of any web source and manipulate it however you like. Here's a quick way to view what's happening in Microsoft's PowerShell blog:

Invoke-RestMethod -Uri http://blogs.msdn.com/powershell/rss.aspx | Select-Object Title, Link | Out-GridView

Process files

To process text files, use Get-Content to read them, manipulate the content how you want and use Set-Content to save changes. Mistakes could result in lost data, though – only work with copies of files until you're sure you know what you're doing.

Run scripts automatically

If you become a PowerShell master, you could even set up Task Scheduler to run scripts automatically, whenever you like. Read up on the New-ScheduledTaskAction, New-ScheduledTaskTrigger and Register-ScheduledTask cmdlets, and see this blog post for an example.

How to automate Windows with PowerShell

Enter PowerShell commands faster

Don't waste time typing long commands at the PowerShell console. Enter just a few characters (Get-Pr for Get-Process, say), keep pressing tab and any matches appear. Or use an alias, a shorter version of the same text; npssc works in exactly the same way as New-PSSessionConfigurationFile, for example.

Enter Get-Alias to see the aliases you can use right now, and Set-Alias to create your own.

How to automate Windows with PowerShell

Extend your skills

Point your browser at Microsoft's Scripting Centre, and look out for interesting sample scripts. Copy and paste these into the ISE, press [F5] to try them out, then read the code and try to figure out how they work – it's a great way to learn the language.