Powershell cheat sheet

Page 1

HTPP://D3NY4LL.BLOGSPOT.COM WORKING WITH FILES

ENVIRONMENT

SYSTEM ADMINISTRATION

Variables & Arrays

Finding in files

Variables Must start with $

To read the contents of a text file into a variable, call the Get-Content

C:\PS>$events = get-eventlog -logname application -newest

a = 32

cmdlet followed by the path to the text file:

100

Can be typed

$a = Get-Content C:\Scripts\Test.txt

C:\PS> $events | select-string -inputobject {$_.message} -pattern "failed"

[int]$a = 32 Each line in the file ends up as an item in the array $a. If you want to To initialise Arrays

access a single line in the file you can simply specify the index number

$a = 1,2,4,8

corresponding to that line:

To query

$a[0]

Processor Time Get-Counter '\Processor(*)\% Processor Time'

10 largest files within a directory structure

$b = $a[3 ]

Constants Created without $ Set-Variable -name b -value 3.142 -option constant

gci . -r | sort Length -desc | select fullname -f 10

This command echoes back the last line in $a:

Kill a Process

$a[-1]

For instance, you’d do the following for BadThread.exe:

Bonus. To determine the number of lines, words, and characters in a

Referenced with $

text file use this command:

$b

How to Access Arguments To access command-line arguments used when starting a script use

if the process number is 23334, so run :

get-content c:\scripts\test.txt | measure-object -line -word -character

stop-process -id 2792

the automatic variable $args. You can cycle through the individual

Top 10 physical memory consumer process

arguments in the $args collection by using code similar to this:

To save data to a text file use the Out-File cmdlet:

foreach ($i in $args) {$i}

Get-Process | Out-File C:\Scripts\Test.txt

To access a particular argument use the collection index number, with

To append data to an existing file, add the –append parameter:

0 representing the first item in the collection, 1 representing the

get-process BadTh*

Get-Process | Sort-Object pm –desc | Select-Object –first 10

Reboot Multiple computers I Like it, you can reboot every computer in the ServerList file.

second item, etc:

Get-Process | Out-File C:\Test.txt –append

$args{0]

gc c:\Temp\ServerList.txt |%{Restart-computer You can also use the MS-DOS redirection characters (> for write, >> for –computername $_ –force}

You can reference the last item in a collection by using the index number –1:

append) when using Windows PowerShell. This command writes data to the file C:\Scripts\Test.txt:

Backup & Delete Event Logs

Get-Process > C:\Scripts\Test.txt

$args[-1]

To 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

How to Make Comparisons 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-

Use ConverTo-Html and>

get-eventlog security | export-clixml -path Seclog.xml

$a = Get-Process $a | Convertto-Html -property Name,Path,Company > test.htm

sensitive equals operator; -clt is the case-sensitive less than operator.

-lt -le -gt -ge -eq -ne -like -notlike

Be carefull !!!

Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to Like (uses wildcards for matching) Not like (uses wildcards for matching)

After you backup each and every eventlog on the machine, you could delete the eventlogs using the below script

Use Export-Csv and Select-Object to filter output>

get-eventlog -list |%{$_.clear()}

$a = Get-Process $a | Select-Object Name,Path,Company | Export-Csc -path test.csv

RESOURCES ONLINE http://microsoft.com/technet/scriptcenter/topics/winpsh/manual/default.mspx

How to Sort Data To sort data returned by Windows PowerShell simply pipe that data to the Sort-Object cmdlet, specifying the property you want to sort by:

http://blogs.msdn.com/PowerShell/

Get-Process | Sort-Object ID You can also add the –descending or -ascending parameters to specify

http://microsoft.com/technet/scriptcenter/hubs/msh.mspx

a sort order: Get-Process | Sort-Object ID –descending

http://microsoft.com/technet/scriptcenter/topics/winpsh/toolbox.mspx

You can even sort by multiple properties: Get-Process | Sort-Object ProcessName, ID

http://download.microsoft.com/download/4/7/1/47104ec6-410d-4492-890b-2a34900c9df2/Workshops-EN.zip

POWERSHELL CHEAT SHEET


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.