Get a Folder Size with PowerShell

It’s really easy to get the size of a folder with PowerShell. Even though the directory does not actually have a property for size, you just need to get the size of all of the files inside the folder.

function Get-FolderSize {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
$Path,
[ValidateSet("KB","MB","GB")]
$Units = "MB"
)
  if ( (Test-Path $Path) -and (Get-Item $Path).PSIsContainer ) {
    $Measure = Get-ChildItem $Path -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum
    $Sum = $Measure.Sum / "1$Units"
    [PSCustomObject]@{
      "Path" = $Path
      "Size($Units)" = $Sum
    }
  }
}

One of my favorite things about this script is the ability to choose to get the folder size in KB, MB or GB.

 

This won’t include any results from items that you don’t have read access to, so be aware of that. Otherwise, it’s a fast and easy way to get the folder size from PowerShell!

How To Find Dell System Tag Using PowerShell

I administer a lot of computer brands, and support a wide ecosystem.  But I see a lot of Dell Laptops, Desktops, and Servers in my normal day-to-day.  A common task is to rebuild a system, or to update a driver after troubleshooting blue screens.

When it’s time to download drivers, there’s one sequence of events that I don’t want to do anymore:

  1. Turn over laptop
  2. Read the serial number
  3. Tell myself “I got this”
  4. Stare at the serial number
  5. Memorize the serial number
  6. Turn the laptop back over, and enter the serial number onto the support webpage
  7. Forget the stupid serial number halfway through (cause it’s the serial number that’s stupid, not me, right?)
  8. Repeat steps 1-7 again (do step 8 only one time.  If today is Monday and you have not had coffee yet, do step 8 two times)
  9. Fine!  Write down the stupid serial number
  10. Tell myself “No problem – I told you I got this”

Now that I’m more refined in my technique, I can show you my simple way to find the serial number or system tag of a Dell computer by using PowerShell:

get-wmiobject Win32_ComputerSystem | select SerialNumber

That actually returns a PSCustomObject.  Now, you might want to get right at the string value instead.  If you’re putting the computers serial number or system tag through the pipeline, or saving it to a file, then you should use this instead – it will return just the string value stored in WMI.

(gwmi Win32_ComputerSystem).SerialNumber

Here, we invoke the alias for get-wmiObject, gwmi.  It is exactly the same, just fewer keystrokes.  Then, we use the parenthesis to turn it into an object, and put the “.” (dot) there to get access to the properties.

This can also be used to get the system tag for a remote system by using the –computername parameter on the get-wmiobject cmdlet

gwmi Win32_ComputerSystem –computername remotesystem-pc

This is the best way that I’ve found to get the information from a Dell computer.  You can use the Service Tag on their website to get up to date drivers and warranty information.

How to Identify Uninstaller Information From Registry Keys

If you want a fast way to get information about the uninstallers that are listed in the registry, then check out this simple script to parse out some information for you.


$searchterm = read-host "Enter search term for uninstallers"
$uninstallers = get-childitem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
$founditems = $uninstallers | ? {(Get-ItemProperty -path ("HKLM:\"+$_.name) -name Displayname -erroraction silentlycontinue) -match $searchterm}
write-host "Searched registry for uninstall information on $searchterm"
write-host "------------------------------------------"
if ($founditems -eq $null) {"None found"} else {
write-host "Found "($founditems | measure-object).count" item(s):`n"
$founditems | % {
Write-host "Displayname: "$_.getvalue("Displayname")
Write-host "Displayversion: "$_.getvalue("Displayversion")
Write-host "InstallDate: "$_.getvalue("InstallDate")
Write-host "InstallSource: "$_.getvalue("InstallSource")
Write-host "UninstallString: "$_.getvalue("UninstallString")
Write-host "`n"
}
}

This little baby asks you for a search term, then grabs the pertinent software uninstall information out of the HKey Local Machine registry hive.

It’s simple and effective,  and I put it together in just a couple of minutes.  It could stand to have some polish on it, but it does the job and it does it like now!

Would you improve it?  How?  Comments are open.

Continue reading “How to Identify Uninstaller Information From Registry Keys”