Sample powershell scripts and commands

SAMPLE COMMANDS/SCRIPTS

– run command to see which folder is the biggest
http://searchwindowsserver.techtarget.com/tip/0,289483,sid68_gci1366742_mem1,00.html
dir C:specifyfolder -Recurse | where {$_.psiscontainer} | select name, fullname ,@{n=”Size”;e={ (dir $_.fullname -r | Measure-Object length -sum).sum }} | sort size -desc

– run script to see which folder is the biggest
http://gallery.technet.microsoft.com/ScriptCenter/en-us/36bf0988-867f-45be-92c0-f9b24bd766fb

– use powershell to build a disk cleanup utility
get-childitem c:scripts -exclude .docx, .pptx -recurse | remove-item
get-childitem c:scripts *draft* -recurse | remove-item

– slowest but possibly best one to remove temp files
http://blogs.technet.com/heyscriptingguy/archive/2006/10/23/how-can-i-use-windows-powershell-to-delete-all-the-tmp-files-on-a-drive.aspx
get-childitem c: -include *.tmp -recurse | foreach ($_) {remove-item $_.fullname}
get-childitem c: -include *.tmp -recurse | foreach ($_) {remove-item $_.fullname -whatif}

– this site has 2 simple scripts to delete temp files
http://www.computerperformance.co.uk/powershell/powershell_temp.htm

– helpful scripts for a DBA
http://www.simple-talk.com/sql/database-administration/why-this-sql-server-dba-is-learning-powershell/

– delete files older than X days in powershell
http://www.winblogs.net/index.php/2009/10/01/delete-files-older-from-in-powershell/

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.