Simple Powershell examples
New-Item $profile -Type File -Force
Navigate to the location of the created file and then create functions i.e.
Function grep {
& 'C:\Documents and Settings\rsailopal\Documents\Powershell1\grep1.ps1'
}
List installed software:
Get-WMIObject -Class Win32_Product | Select-Object -Property Name
Parse logs:
Get-Eventlog -List
Get a list of the log types
Get-WinEvent -Logname Application -MaxEvents 5
Get the last 5 application errors
Remote Connections
Enable-PSRemoting -Force
Enable remote connection to machine
$hostnme = hostname
Invoke-Command -ComputerName $hostnme -ScriptBlock { hostname }
Execute the command hostname on the localhost
$hostnme = hostname
$s = New-PSSession -ComputerName $hostnme
Invoke-Command -Session $s { hostname }
Execute the same command creating a persistent session (variables created remotely are persisted across Invoke-Command calls)
Environmental Variables
Get-ChildItem -Path Env:
Get environmental variables.
Memory usage
systeminfo | ForEach-Object { If ( $_ -like "*Memory*") { Write-Host $_ } }
CPU utlisation
Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select Average