powershell cheat sheet
DESCRIPTION
PowerShell Cheat SheetTRANSCRIPT
WORKING WITH FILES
To read the contents of a text file into a variable, call the Get-Content cmdlet followed by the path to the text file:
$a = Get-Content C:\Scripts\Test.txt
Each line in the file ends up as an item in the array $a. If you want to access a single line in the file you can simply specify the index number corresponding to that line:
$a[0]
This command echoes back the last line in $a:
$a[-1]
Bonus. To determine the number of lines, words, and characters in a text file use this command:
get-content c:\scripts\test.txt |
measure-object -line -word -character
To save data to a text file use the Out-File cmdlet:
Get-Process | Out-File C:\Scripts\Test.txt
To append data to an existing file, add the –append parameter:
Get-Process | Out-File C:\Test.txt –append
You can also use the MS-DOS redirection characters (> for write, >> for append) when using Windows PowerShell. This command writes data to the file C:\Scripts\Test.txt:
Get-Process > C:\Scripts\Test.txt
How to Access Arguments
C:\PS>$events = get-eventlog -logname application -newest
100
C:\PS> $events | select-string -inputobject {$_.message} -pattern "failed"
ENVIRONMENT
To access command-line arguments used when starting a script use the automatic variable $args. You can cycle through the individual arguments in the $args collection by using code similar to this:
foreach ($i in $args) {$i}
To access a particular argument use the collection index number, with 0 representing the first item in the collection, 1 representing the second item, etc:
$args{0]
You can reference the last item in a collection by using the index number –1:
$args[-1]
Finding in files
SYSTEM ADMINISTRATION
Use ConverTo-Html and>$a = Get-Process
$a | Convertto-Html -property Name,Path,Company > test.htm
Use Export-Csv and Select-Object to filter output>$a = Get-Process
$a | Select-Object Name,Path,Company | Export-Csc -path test.csv
Created without $Set-Variable -name b -value 3.142 -option constant
Referenced with $$b
Variables & ArraysVariables Must start with $a = 32
Can be typed[int]$a = 32
To initialise Arrays$a = 1,2,4,8
To query$b = $a[3 ]
Constants
Windows PowerShell cmdlets (like Where-Object) use a special set of comparison operators, including those shown in the following table.
Each of these operators can be made case sensitive by adding a c immediately after the hyphen. For example, -ceq represents the case-sensitive equals operator; -clt is the case-sensitive less than operator.
-lt Less than-le Less than or equal to-gt Greater than-ge Greater than or equal to-eq Equal to-ne Not equal to-like Like (uses wildcards for matching)-notlike Not like (uses wildcards for matching)
How to Make Comparisons
http://blogs.msdn.com/PowerShell/
http://microsoft.com/technet/scriptcenter/hubs/msh.mspx
http://microsoft.com/technet/scriptcenter/topics/winpsh/manual/default.mspx
http://microsoft.com/technet/scriptcenter/topics/winpsh/toolbox.mspx
RESOURCES ONLINE
HTPP://D3NY4LL.BLOGSPOT.COM
How to Sort DataTo sort data returned by Windows PowerShell simply pipe that data to the Sort-Object cmdlet, specifying the property you want to sort by:Get-Process | Sort-Object ID
You can also add the –descending or -ascending parameters to specify a sort order:Get-Process | Sort-Object ID –descending
You can even sort by multiple properties:Get-Process | Sort-Object ProcessName, ID
Get-Counter '\Processor(*)\% Processor Time'
Processor Time
gci . -r | sort Length -desc | select fullname -f 10
10 largest files within a directory structure
Kill a ProcessFor instance, you’d do the following for BadThread.exe:
get-process BadTh*
if the process number is 23334, so run :
stop-process -id 2792
Get-Process | Sort-Object pm –desc | Select-Object –first 10
Top 10 physical memory consumer process
http://download.microsoft.com/download/4/7/1/47104ec6-410d-4492-890b-2a34900c9df2/Workshops-EN.zip
I Like it, you can reboot every computer in the ServerList file.
gc c:\Temp\ServerList.txt |%{Restart-computer –computername $_ –force}
Be carefull !!!
Reboot Multiple computers
Backup & Delete Event LogsTo backup eventlog, we could use get-eventlog cmdlet to retrieve the entries in the eventlog and then using export-clixml cmdlet to store them in a xml file
get-eventlog security | export-clixml -path Seclog.xml
After you backup each and every eventlog on the machine, you could delete the eventlogs using the below script
get-eventlog -list |%{$_.clear()}
POWERSHELL CHEAT SHEET