Saturday, March 02, 2013

Powershell One Liners

Instead of making a post every time I stumble upon a cool powershell one liner, I'll combine them all in this post. If you have a better one liner for my tasks, please comment.

Kill a process by name (In this case Jenkins).
 Get-Process | ? {$_.Name -like 'JenKins' }  | Stop-Process

Delete all Duplication Downloads (I usually run it first without the del paramaters)
 dir  -r  | ? {$_.Name -like '(1)' }  | del

Powershell 3, Where/ForEach hack - allowing you to specify properties sans an anonymous expression.
 dir  -r  | ?  Name -like '(1)' 

Restart Explorer (often auto-hide of taskbar gets stuck, restarting explorer fixes it.)
Get-Process | where name -eq explorer  |kill ; explorer

Find Who's hogging memory
 Get-Process | sort "VM" -Des | Select-Object -first 10

Set current time  forward 4 hours
 set-date ((get-date) + [TimeSpan]::FromHours(4.0))

Get my son's age in weeks
 ((get-date) - (get-date 4/22/2010)).TotalDays/7

1-Liners relying on  Power Shell Community Extensions (pscx)

Load pscx
  Import-Module Pscx

Load the VS environment:
  Import-VisualStudioVars 2012

Put stuff on/off the clipboard:
  PS C:\hgs\igsmilebox> dir *ts | Out-Clipboard
  PS C:\hgs\igsmilebox> Get-Clipboard

Get Time since last reboot:

PS C:\Users\idvor_000\SkyDrive\Documents> uptime

Uptime                                                         LastBootUpTime
------                                                         --------------
6.19:20:27.1723192                                             4/27/2013 10:01:13 PM

No comments: