powershell2

PowerShell learning notes

BASIC COMMANDS
cls // clean screen
get-history // list history of commands
get-help command-full // show additional info
get-command -noun // search for second name in powershell naming convention *-noun
get-command -verb // searching for first name in powershel naming convention verb-*

-whatif // parameter will show what will happen when it will actually execute it
supported by most of commands

sort-object -property SMTH // sort by specific column
get-member // output is showing what type and additional information about command output
#get-process | get-member
$_ // is current object processes
where-object {$_.SMTH -gt SMT } | foreach-object { $_.SMTH + ” will be written to output” }
out-file // set output to file

EXECUTOION POLICY
Get-ExecutionPolicy
Set-ExecutionPolicy remotesigned // run local policies but remote has to be signed
The most used commands
Where-Object
Foreach-Object
Get-Help
About_Execution_Policies
About_Operators
About_Common_Parameters
About_Pipelines
About_Scripts
About_*
clip // save output to clipboard

filesystem commands
push-location == pushd // save location to stack
pop-location == popd // pop location from stack
get-psdrive // all drives + certificate store, registries, etc.
new-item -type file // creates new file
cat file // see file
test-path URI // verify if exist
invoke-item == ii // same as double-click in explorer
$profile // define session used for current user, ISE has different one
new-alias -name SHORTCUT -value EXISTINGCOMMAND // define alias for existing command
resolve-path // get full path from short form

accesing .net from PS
$PSVersionTable // list available .net runtimes
// PS 2 == .NET 4
// creating new object of specific type
new-object -typename FULLYQUALIFIEDNAME – argumentlist ARGUMENTSFOR INITIALIZATION
-property @{K:V; K2:V2}
[System.SOME.TYPE] // reference to specific type
[Envitonmnet]::GetLogicalDrives() // executing static method
register-objectevent -inputobject $someObject -envetname `EVENTNAME` // subscribe to events
get-event // once event is raised, they are stacked – needs to be removed when processing
remove-event // remove clear
wait-event // wait till event it’s
get-eventsubscriber // list all subscription
add-type -assemblyname ASSEMBLYNAME // load assembly from GAC
add-type -path SOMEPATH.DLL // load assembly from path
import-module // loading assemblies that contain Poweshell features
param(PARAM1, PARAM2, …) // defines which parameters are required
get-help
About_Objects
New-Object
About_Type_Operators
Add-Type
Import-Module
Register-ObjectEvent
Get-Event
About_Scripts
About_Parameters
technet.microsoft.com/scriptcenter
codeplex.com
poshcode.org // only contains powershell code
~/Documents/WindowsPowerShell/Modules/ModuleName // module has to be placed here, then are visible with get-module -ListAvailable

interesting commands
out-clipboard // set output to clipboard

Leave a Reply