Table of Contents


Piping ( | )

Much like in UNIX (The old Linux command line programming language) within PowerShell you are able to string multiple commands from one command to another for further manipulation. As an example the command 'Get-ADuser -identity sumeet.singh -property memberof' will output the command for retriving a user object in an Active Directory environment along with the expanded property of the object memberof. 'memberof' indicates the security groups that the user object has.

In PowerShell, when running commands, many times the complete command ouput will not display on the screen unless explicity asked for as in this case though the switch '-property memberof' this is because the complete output is redundant in many common system admin tasks. By using the wild card command '*' as a string in the '-property' field e.g. '-property *' we can then have a complete output of the get-aduser command object.

Going back to our previous command, when ouputinng the 'get-aduser' command, we would want to further analyze by gathering only the Security Groups in the output. This can be achieved by ouputting the contents of the PowerShell get-aduser command into another command by piping using the character '|'. The pipe character alows strining multiple commands from left to right of the CLI as its written.

The full output of only finding the Security Group permissions of user sumeet.singh is;
> Get-Aduser -identity sumeet.singh -properties memberof | select-object -expandProperty memberof

By running the command above, we; 1) Retrieve the AD Object sumeet.singh 2) Retrieve with that output the extra hidden property contents of Security Group Permissions 3) Pipe the output to command 'select-object' that includes a property '-expandProperty' to only output the pipeline property input 4) Output the contents of the second command, thus only retrieving the users Security Groups.

Parallel Commands ( ; )

PowerShell can also run identical commands together using the semicolon character ";". In Instance below, the command is running the UNIX command Print Working Direcotry, which outputs the filepath of the directory the current CLI is in, and the second command will retrieve the AD Object sumeet.singh.

> pwd ; get-aduser -identity sumeet.singh


Not all commands can be piped from left to right one after another, and not all commands can accept the property input of the previous command. Knowing how a command accepts pipeline inputs is vital to building a script or program. The 'Get-Member' command is required to know what a command can input and output and you will need to be able to read the ouput of the 'Get-Member' command to work with PowerShell. To use, the Get-Member command

History & Clipboard

The history of last run commands can be found by using the
command. The parameter "-Id" specifies the exact record in the command history you would like to retrieve. It may then also be copied onto the clipboard by using the
command. e.g.

> Get-ChildItem
> history -Id 1 | select -expandProperty commandline | clip

Escaping Characters ( ' , " )

When encapsulating alphanumerical and special characters in a variable or as a single string, they will be requried to be encapsulated with quotations before being read by PowerShell. This means long strings of texts such as website URL's or name and subexpressions combo characters in a single variable will need to have their start and end enclosed with Single or double quotation marks.
Single quotation marks ( ' ) represent anything that is contained within is interpreted literally. Therefore a long string of spaces encapsulated in single quotations ( ' ) will be that complete string. E.g. The command

$username = 'Username $env:USERNAME'

will output the following when called

> $username
Username $env:USERNAME

The double quotation marks ( " ) in PowerShell is the expanding string character. This means that variable values and subexpressions are evaluated inside the double quotation marks. This allows variables to run complex commands within a string for meaningull information output. E.g.

> $username

Username Sumeet.Singh

When encapsulating a single string with multiple characters that contain special characters that include a quotation mark within it, such as a name with a quotation mark e.g. Mark O'Neil, Double quotation marks ( " )will need to be used to encase the single or double quotation mark within the string. e.g. To encapsulate the name 'Mark O'Neil' in a single variable including the single quotation precursing the word .Neil', the quotation mark will need to be encapsulated with Double quotations.

> $name = "Mark O'Neil"
> $name

Mark O'Neil

When the string has a double quotation mark within it that needs to be lieterally read, then a Single double quotation mark ( " ) will be required next to the double quotation mark within the string to have PowerShell read the character as a literal string character. It doesnt matter if the variable is then encased in single or double quotation marks. e.g. e.g. To encapsulate the name 'Mark O"Neil' in a single variable including the double quotation precursing the word .Neil', an double quotation mark will need to be added beside the existing character within the string. e.g.

> $name = 'Mark O""Neil'
> $name

Mark O"Neil

or double quotation marks will still work aslong as an double quotation mark is added to escape each double quotation in the string.

> $name = "Mark O""Neil"
> $name

Mark O"Neil

You may explore more on Escaping characters at the official Microsoft Knowledge Base Article located here

Here Strings

You may explore more on Here Strings at the official Microsoft Knowledge Base Article located here





and the keyboard shortcut. Difference between intelligence on CLI vs IDE. Typing into ISE before runnings


through get-alias -name %

PowerShell (Cmdlet) PowerShell (Alias) Windows Command Prompt Unix shell Description
Get-ChildItem gci, dir, ls dir ls Lists all files and folders in the current or given folder
Test-Connection N/A ping ping Sends ICMP echo requests to the specified machine from the current machine, or instructs another machine to do so
Get-Content gc, type, cat type cat Gets the content of a file
Get-Command gcm help type, which, compgen Lists available commands
Get-Help help, man help apropos, man Prints a command's documentation on the console
Clear-Host cls, clear cls clear Clears the screen
Copy-Item cpi, copy, cp copy, xcopy, robocopy cp Copies files and folders to another location
Move-Item mi, move, mv move mv Moves files and folders to a new location
Remove-Item ri, del, erase, rmdir, rd, rm del, erase, rmdir, rd rm, rmdir Deletes files or folders
Rename-Item rni, ren, mv ren, rename mv Renames a single file, folder, hard link or symbolic link
Get-Location gl, cd, pwd cd pwd Displays the working path (current folder)
Pop-Location popd popd popd Changes the working path to the location most recently pushed onto the stack
Push-Location pushd pushd pushd Stores the working path onto the stack
Set-Location sl, cd, chdir cd, chdir cd Changes the working path
Tee-Object tee N/A tee Pipes input to a file or variable, passing the input along the pipeline
Write-Output echo, write echo echo Prints strings or other objects to the standard output
Get-Process gps, ps tlist,tasklist ps Lists all running processes
Stop-Process spps, kill kill, taskkill kill[e] Stops a running process
Select-String sls findstr find, grep Prints lines matching a pattern
Set-Variable sv, set set env, export, set, setenv Creates or alters the contents of an environment variable
Invoke-WebRequest iwr, curl, wget N/A wget, curl Gets contents from a web page on the Internet

Visit next page to learn about - Profiles