PowerShell: F is for Format Operator

PS> "{2} {0}" -f "there", "ignore", "hi" hi there   Operator Example Results Description {0} Display a particular element "{0} {1}" -f "a", "b" a b {0:x} Display a number in Hexadecimal "0x{0:x}" -f 181342 0x2c45e {0:X} Display a number in Hexadecimal uppercase "0x{0:X}" -f 181342 0x2C45E {0:dn} Display a decimal number left justified, padded with zeros "{0:d8}" -f 3 00000003 {0:p} Display a number as a percentage "{0:p}" -f .123 12.30 % {0:c} Display a number as currency "{0:c}" -f 12.34 $12.34 {0,n} Display with field width n, left aligned "|{0,5}|" -f "hi" | hi| {0,-n} Display with field […]

Read more

PowerShell Folder/Directory Creation

Script: $FldrName   = "NewFolder" $FldrParent = "C:Temp-New" $FldrPath   = $FldrParent + $FldrName [IO.Directory]::CreateDirectory($FldrPath)   Output: Mode                LastWriteTime     Length Name                                                                                                            —-                ————-     —— —-                                                                                                            d—-         11/9/2011  10:53 AM            NewFolder                                                                                                       Will create the whole path all along the specified folder location. It uses .net method Directory.CreateDirectory.   Script: $FldrName   = "NewFolder" $FldrParent = "C:Temp-New" $FldrPath   = $FldrParent + $FldrName $DirObj     = [IO.Directory]::CreateDirectory($FldrPath) Write-Host "LISTING ALL _PROPERTIES_ ACCESSIBLE VIA CreateDirectory OBJECT METHOD" ForEach ($attribute in ($DirObj | Get-Member -membertype properties | sort name )) {        $atrrib = $attribute.Name         "{0,-20}: {1}" -f $atrrib, $DirObj.$atrrib }   Output: LISTING ALL _PROPERTIES_ ACCESSIBLE VIA […]

Read more

Powershell Process Explorer

Get count of running processes on Powershell command line: PS C:> @(Get-Process | ? { $_.ProcessName -eq “winlogon” }).Count 8 PS C:> Get Detailed view of process explorer on Powershell command line: PS C:> Get-WmiObject Win32_Process -Filter “Name like ‘%excel%’” | select-Object ProcessName, GetOwner, ProcessId, ParentProcessId, VirtualSize, CommandLine | Sort-Object $_.ProcessName -Descending | ft -auto ProcessName GetOwner ProcessId ParentProcessId VirtualSize CommandLine ———– ——– ——— ————— ———– ———– EXCEL.EXE                 7780            2972   205893632 “C:Program Files (x86)Microsoft OfficeOffice12EXCEL.EXE” /e PS C:> Exporting/Saving the results to Excel: PS C:> Get-WmiObject Win32_Process | select-Object ProcessName, GetOwner, ProcessId, ParentProcessId, VirtualSize, CommandLine | Sort-Object $_.ProcessName -Descending | […]

Read more