Scripting >> Powershell >> 5.1 >> How to perform common tasks

 

Show powershell version

method 1

> Get-Host

method 2

> $PSVersionTable.PSVersion

method 3

> (Get-Host).version

sleep (pause) for x seconds start-sleep -s timeInSeconds
Output string to the screen write-host String
Join / concatenate string1 & string2 $newString = "Hello " + "world"
check current execution policy get-executionpolicy
set new execution policy on a powershell session started with admin rights

set-executionpolicy Restricted|AllSigned|RemoteSigned|UnRestricted
 
Replace parts of a string

$old = "quick brown fox"

# replace one string
$new = $old.replace("quick","slow")

# replace two strings
#new = $old.replace("quick","slow").replace("brown","black")
 

Test if a path/dir/file exists

i

 

f ( Test-Path $path )
{
    # processing when the specified path exists
}
else
{
    # when  path does not exist
}
Import CSV

$csvdata = (import-csv "path-to-csv-file" | where {$_.file1 -eq "file1-filter" -and $_.field2 -eq "file2-filter" | select comma-separated-list-of-fields

 

Show software packages installed in the system


## show all packages

Get-Package


## show package matching a wildcard

Get-Package wildcard

e.g.

Get-Package McAfee*