Powershell Notes

These are my notes for lecturing about PowerShell. I am just following the tutorials here:


Lesson 1: PowerShell Console

Powershell is object-based, so data can be piped from one object to another without tedious reformatting.

Launch it from a command prompt with POWERSHELL
Exit with EXIT

Customize a shortcut to point to a home directory

Create a directory C:\scripts
Click Start and type in POWERSH
Right-drag "Windows PowerShell" to the desktop and drop it there.
Click "Create Shortcut(s) Here"
Right-click the "Windows PowerShell" icon on your desktop and click "Properties"
In the "Start In:" text box, type C:\SCRIPTS
Click OK
Double-click the shortcut to launch PowerShell
Try these commands:
Tab Completion
Up-arrow for history list
Case insensivity
F7 for history list

Lesson 2: PowerShell Cmdlet

Verb-Noun format

Get-Command shows all commands
Get-Command -Verb Get shows all commands that use the verb "Get"
Get-Command -Noun History shows all commands that use the noun "History"

Get-Help shows help about Get-Help, on one long page
Help shows help about Get-Help, one page at a time
Help * shows all help topics

Get-Command -Noun Service shows all commands that use the noun "Service"
Get-Service shows all services

services.msc works because native Windows commands work from Powershell

Get-Service -name Browser
Stop-Service -name Browser

It fails unless you ran PowerShell as an Administrator

Get-Service -name Browser

Lesson 3: PowerShell Aliases

Get-Alias shows built-in aliases

Note that ls and dir and gci are all aliases for Get-ChildItem.

Lesson 4: PowerShell Parameters, Objects, and Formatting

Set-ExecutionPolicy Unrestricted -whatif shows what would happen
Set-ExecutionPolicy Unrestricted -confirm asks user for confirmation
Suspend changes prompt to allow you to execute more commands before confirming, such as Set-ExecutionPolicy and Unrestricted to see what it will do. Exit leaves Suspend mode.

Objects have Properties and Methods.

Get-Service | Get-Member shows Properties and Methods of the "Get-Service" cmdlet

Get-Service | Get-Member -MemberType Property shows Properties of the "Get-Service" cmdlet

Get-Service | Get-Member -MemberType Method shows Methods of the "Get-Service" cmdlet

Notice the Name property.

Get-ChildItem -Path C:\ shows all files and folders in C:\

Get-ChildItem -Path C:\ -Recurse shows all files and folders on the C:\ drive

Get-ChildItem -Path C:\ -Recurse | Where-Object {$_.LastWriteTime -gt "5/1/2011"} shows all files and folders on the C:\ drive that were modified after May 1, 2011

Get-ChildItem -Path C:\users -Recurse | Where-Object {$_.LastWriteTime -gt "5/1/2011"} shows all files and folders in the C:\users folder that were modified after May 1, 2011

Formatting Output

Get-Command Format-* shows the four format types
Get-ChildItem C:\Windows | Format-Table outputs a table
Get-ChildItem C:\Windows | Format-Table -AutoSize adjusts column sizes
Get-ChildItem C:\Windows | Format-List shows several properties
Get-ChildItem C:\Windows | Format-List -Property FullName, LastWriteTime shows specified properties

Get-ChildItem | Get-Member shows all properties and methods available (directory must not be empty)

Get-EventLog System | Group-Object eventid | Sort-Object Count -descending sorts event log with most numerous events first

Get-Process | ConvertTo-html displays raw HTML, not much use
Get-Process | ConvertTo-html | out-file "Processes.html" creates an HTML file

Get-Process | Export-CSV Processes.csv makes a CSV file

Last modified: 11-15-2012