Book One
Script Compatibility
Code written for PowerShell Core should usually (hopefully) run as is on Windows PowerShell too, but the reverse is often not true.
In your scripts, you can test for which edition of PowerShell is being used:
There are five main Windows PowerShell "hosts" from Microsoft:
PowerShell Console
PowerShell ISE
Visual Studio Code
Windows Admin Center
Powershell Web Access
Information about the hosting process for the PowerShell engine in front of you right now can be displayed by using the built-in $host variable:
PowerShell Core Host Processes
PowerShell Console
Visual Studio Code
To see what version of PowerShell you are running now:
To see which versions of PowerShell the current running shell is compatible with:
64-bit (x64) versus 32-bit (x86)
To test whether the OS and/or PowerShell are running the 64-bit versions:
Tips for Executing Commands
Here are some tips for executing commands in PowerShell.
Your New Best Friend: Tab Completion
The key habit to learn in PowerShell is using tab completion. By typing a few letters of a cmdlet, function, parameter, or file, you can press the Tab key to auto-complete the name. This speeds up typing, reduces errors, and prevents fatigue from typing long names.
Tip: If you accidentally hit the Tab key too fast and go past what you wanted, hit Shift-Tab to go backward to the prior options
Put "." in Front of Scripts or Executables in the Current Directory
Script Execution Policy
If you cannot run any scripts at all, run the following command (you only have to do it once):
Open Script in New Tab
A fast way to open a text file in a new tab in PowerShell ISE is from the command line:
Run Selection in PowerShell ISE
In PowerShell ISE, if you open a script for editing, you can highlight one or more lines from that script and execute just those highlighted lines.
Running Native Commands
All of the following commands work in PowerShell just like they do in CMD:
To see what native commands must be run in powershell.exe, not ISE, run:
Piping ( | ) and Redirection Operators ( >, >>)
When you want to feed the output of one command into a second command as input, use the pipe ("|") operator.
If you want to redirect (">" or ">>") the output of a command to a file, it'll usually work as expected, but it is better to use cmdlets like Out-File and Export-Csv instead.
Separate Multiple Commands on One Line with Semicolons
If you want to run multiple commands with a single line, just separate each command with a semicolon. A semicolon at the very end of a line or command is not required.
Getting Help in PowerShell
To get help about the get-help cmdlet itself:
To see a list of all cmdlets, aliases, providers, and help files:
To see a listing of cmdlets that match a particular pattern:
To get more detailed help for a cmdlet, including parameter syntax and examples:
To pop up a searchable graphical window to display the help text:
Update Help
To update the local help files from Microsoft, if you have internet access:
HELP and MAN
Get-Help About_Help Topic
To see a listing of all help files:
The following command will open a separate graphical window with the help text, and it includes a search box too:
The following command will open a separate graphical window with the help text, and it includes a search box too:
Aliases
An "alias" is an alternative name for a frequently-used cmdlet, function, script, file, or executable.
Alias | Cmdlet |
---|---|
dir | Get-ChildItem |
ls | Get-ChildItem |
cls | Clear-Host |
echo | Write-Output |
type | Get-Content |
del | Remove-Item |
ps | Get-process |
cd | Set-Location |
pwd | Get-Location |
sort | Sort-Object |
cp | Copy-Item |
where | Where-Object |
? | Where-Object |
% | ForEach-Object |
kill | Stop-Process |
gm | Get-Member |
select | Select-Object |
To see all the aliases defined in the current shell:
To see how a particular alias, like "cd", is mapped:
To create a new alias for notepad.exe named "nn":
To change an existing alias to point toward some other target:
To read more about aliases and how to manage them:
Objects, Properties, and Methods
Let's get an object that represents the LSASS.EXE process:
And a process object also has an interesting and dangerous method:
Get-Member (Alias: gm)
To show properties, methods, and class type:
If you have captured the output of a command to a variable, but you don't know what kind of data it is or what its members are, pipe that variable into get-member:
To see the members of the first item in an array and then see the members of the array itself (an array of objects is of type "System.Object[]"):
Drives and Environment Variables
"Drives" are more than just disk volumes. To see your currently available drives:
To see your currently installed providers and their corresponding drive names:
Cmdlets for moving around in drives of any type, from any provider, with full support for relative path addressing (using "." and "..") and tab completion:
get-location (aliases: gl, $pwd)
set-location (aliases: sl, cd, chdir)
Cmdlets for manipulating items in drive containers:
new-item (alias: ni, but see also mkdir)
remove-item (aliases: ri, del, rm, rd, rmdir)
move-item (aliases: mi, move, mv)
copy-item (aliases: cpi, copy, cp)
rename-item (aliases: rni, rn, ren)
Cmdlets for accessing the properties of an item or multiple items:
get-item (alias: gi)
get-childitem (aliases: gci, dir, ls)
set-item (alias: si)
clear-item (alias: ci)
Cmdlets for accessing an individual property of an item:
get-itemproperty (alias: gp)
set-itemproperty (alias: sp)
clear-itemproperty (alias: clp)
rename-itemproperty (alias: rnp)
move-itemproperty (alias: mp)
remove-itemproperty (alias: rp)
copy-itemproperty (alias: cpp)
Cmdlets specifically for working with strings and the contents of files:
get-content (aliases: gc, type, cat)
set-content (alias: sc)
add-content (alias: ac)
clear-content (alias: clc)
Creating New PowerShell Drives
To map the "share:" drive to the "\localhost\c$" UNC network path:
Last updated