Thursday, December 19, 2013

Learning Power Shell

Get Powershell Version
$PSVersionTable.psversion
$psversiontable

Get a subset of lines from a big text file
http://technet.microsoft.com/en-us/library/hh849787.aspx

Get-Content -Path my.csv | Select-Object -Index(50986)
Get-Content [-Path] <String[]> [-Credential <PSCredential> ] [-Exclude <String[]> ] [-Filter <String> ] [-Force] [-Include <String[]> ] [-ReadCount <Int64> ] [-Tail <Int32> ] [-TotalCount <Int64> ] [-UseTransaction] [ <CommonParameters>]

Get-Content c:\scripts\test.txt | Foreach-Object {Get-Wmiobject -computername $_ win32_bios}
The default pipeline variable $_

Use the Measure-Object cmdlet to easily count the number of lines 
Get-Content c:\scripts\test.txt | Measure-Object

Only the first x number of lines
Get-Content c:\scripts\test.txt -totalcount 5
To get the last five lines:
Get-Content c:\scripts\test.txt | Select-Object -last 5

-Tail(from PowerShell 3.0)
Gets the specified number of lines from the end of a file or other item.

Get-Content c:\Logs\Log060912.txt -TotalCount 50 | Set-Content Sample.txt

(Get-Content Cmdlets.txt -TotalCount 5)[-1]

dir .\*.txt | ForEach {Get-Content $_ -Head 1; Get-Content $_ -Tail 1}



No comments:

Post a Comment