March 8, 2010

Powershell: List the size of folders or sub folders

Filed under: Script — Tags: — Kevmar @ 6:29 am

I was wanting to see how big my users’ home folders are.  With out quotas in place, I decided to use powershell.  I put together a function that will list the size of a folder that I pass to it. I wrote it in a way that if I pass a collection of folders to it, it will list the sizes of those folders individually.   This way I can spot check individual folders or check every folder in the home folders location.

This works even better for me because I need it to list the sub folder of each folder in the home folder location.  My home folders are placed in department folders.  So I have to add that extra step to every script I use to make sure it processes at the right level.  Allowing my function to work on a collection of folders allows powershell to handle this for me. Here is the syntax in action:

#Single folder
PS> Get-Item . | Get-FolderSize

#Sub folders
PS> Get-ChildItem . | Get-FolderSize

#Sub Sub Folders
PS> Get-ChildItem . | %{Get-ChildItem $_.FullName} | Get-FolderSize

It is required that you pass your directory objects to it.  I may update it to allow you to pass a folder path later. But for my needs, this is exactly how I will use it in my scripts.  Here is the full code for Get-FolderSize:

function Get-FolderSize ($_ = (get-item .))  {
  Process {
    $ErrorActionPreference = "SilentlyContinue"
    $length = (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty Name ($_.Name)
    $obj | Add-Member NoteProperty FullName ($_.FullName)
    $obj | Add-Member NoteProperty Length ($length)
    $obj | Add-Member NoteProperty MB ("{0,13:N2}" -f ($length / 1MB))
    Write-Output $obj
  }
}

March 5, 2010

Powershell: Move files with the pipe

Filed under: Uncategorized — Kevmar @ 6:28 am

One of the games I play has a built in screen shot capture button. Every time you press it, a jpg gets saved into the screenshot folder. I have lots of files in this folder and its hard to manage. When I am taking screen shots for a blog post on another site, I will move all that I take that day to another folder. To day I broke out the powershell to do it for me. There are many ways to do this but I was playing with using the pipeline to do things tonight and I ended up with this command:

PS> ls C:\Program Files\World of Warcraft\Screenshots\*.jpg | sort-object lastwritetime | select -last 14 | move -Destination .\BlogFolder

March 4, 2010

Powershell: WGet as Get-WebPage

Filed under: Script — Tags: — Kevmar @ 5:10 am

WGet is an old favorite of mine.  It is very easy to replicate in powerhshell by calling out to the system.web.WebClient object.

function Get-WebPage{
  Process {
   if($_ -ne $null){
      Write-Output (new-object system.net.WebClient).DownloadString($_)
    }
    elseif ($args[0] -ne $null){
      Write-Output (new-object system.net.WebClient).DownloadString($args[0])
    }
  }
}

Get-WebPage "http://www.ithinkincode.com"

All this gives you is the raw text of the page. You can then pipe it into a file or into another function.

March 2, 2010

Powershell Grep to find old server names in batchfiles

Filed under: Script,Uncategorized — Tags: — Kevmar @ 11:40 am

I was troubleshooting a user access problem today and found that they had a old mapping to an old server in the login script.  We just moved all our data to a different server so it was expected to have a few issues like this.  We do use DFS for most of our shares so the transition was very smooth for most people.  Just a few exceptions needed fixed.

I think we just over looked the log in scripts so I needed a script to check them all.  This is where I should just use grep but I wanted to see how to do it in powershell.  Here is the command I came up with:

PS> get-childitem \\domain\netlogon -include *.bat -recurse | select-string "oldserver"

Here is another way to do the same thing with a few shorcuts that could be easier to remember at the command line:

PS> cd \\domain\netlogon
PS> dir -recurse | select-string "oldserver"

dir is short for get-childitem.  If all you have are .bat files in netlogon you can skip the include.  If you have other install files or packages, use the include to save time.

Powered by WordPress