Basic PowerShell Command-Lets for System Administrators

Find the version of PowerShell you are working in

PS C:> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
—–  —–  —–  ——–
2      0      -1     -1

PS C:> $PSVersionTable | Format-Table -AutoSize

Name                      Value
—-                      —–
CLRVersion                2.0.50727.5466
BuildVersion              6.1.7601.17514
PSVersion                 2.0
WSManStackVersion         2.0
PSCompatibleVersions      {1.0, 2.0}
SerializationVersion      1.1.0.1
PSRemotingProtocolVersion 2.1

PS C:>

Finding basic system details like computer name, Logged-on domain, system path, system architecture x86 or x64 bit?

PS C:> $env:computername
GOVARDHAN-PC
PS C:>

PS C:> $sysinfo = systeminfo
PS C:> $lines = $sysinfo -split "n"
PS C:> $lines | ? { $_ -match "System Type" }
System Type:               x64-based PC
PS C:>

PS C:> $lines | ? { $_ -match "System Type" -or $_ -match "Logon Server" }
System Type:               x64-based PC
Logon Server:              \TESTIND-DC01
PS C:>

PS C:> $env:path
%SystemRoot%system32WindowsPowerShellv1.0;C:oracleora81bin;C:Program Files (x86)Oraclejre1.1.7bin;C:Program FilesCommon FilesMicrosoft SharedWindows Live;C:Program Files (x86)Common FilesMicrosoft SharedWindows Live;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:Program Files (x86)Windows LiveShared;C:WindowsSystem32WindowsPowerShellv1.0
PS C:>

Execute a system command, capture its output and filter for required data:

Just in the above example, I’ve presented running a system command i.e., systeminfo, captured its output to a string variable, parsed through the output to filter required data.

 

Execute a system command, capture its output to a file and open the contents for reading:

PS C:> Invoke-expression "C:WindowsSystem32systeminfo.exe >> C:Temppsipconfig.txt"
PS C:> Test-Path C:Temppsipconfig.txt
True
PS C:> gci C:Temppsipconfig.txt

    Directory: C:Temp

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—         5/10/2013   9:11 PM      25294 psipconfig.txt

PS C:> get-content C:Temppsipconfig.txt | Out-GridView
PS C:>

image

How to find top memory/CPU consuming processes on a given system

PS C:> Get-Process -ComputerName . | Sort-Object CPU -Descending | Select-Object -First 5

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
——-  ——    —–      —– —–   ——     — ———–
   3197     119   207476     154788   558 2,154.31   3544 chrome
   3693     163   119640      98604   579 1,140.91   1816 OUTLOOK
   1807      96    95352      60040   840   665.48    628 explorer
   1198     134   333452     251332  1134   477.50   6508 ScriptEditor
    316      51   150668     148092   581   461.37   4164 chrome

PS C:>

PS C:> Get-Process -ComputerName TestSRV1 | sort CPU -Descending | Select-Object -First 5

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
——-  ——    —–      —– —–   ——     — ———–
    449      21    49604      49176   564     2.11   2160 powershell
    529      34    16320      34740   167     1.52   2100 explorer
    181      27     8348      13484   117     0.31   2420 taskhost
     35       5     1772       4080    47     0.25   2436 conhost
    105      11     2208       7924   102     0.14   2440 taskmgr

PS C:>

 

Viewing tabular contents of the command outputs in GUI:

PS C:> Get-Process -ComputerName . | Sort-Object CPU -Descending | Select-Obj
ect -First 5 | Out-GridView
PS C:>

image

Leave a Reply

Your email address will not be published. Required fields are marked *