StevensNet.com

...Because someone's gotta tell the story...

Blogs

To return to the main Blog List, click Full Blog Listing.

Tags

PowerShell
SMB

Properly parsing formatted data

Friday, May 13, 2016 in Powershell (Views: 1062)
So, here's a scenario where there are probably thousands of references on the internet - calculating folder size in PowerShell. For those of you who need a refresher, here's a sample line of code:

$colItems = (Get-ChildItem 'C:\SomeFolder' -Recurse | Measure-Object -property length -sum)

So, this comes out in bytes - not like anyone wants to see this anymore, maybe 25 years ago when megabyte sized files were considered insane.

So, to format this, you can use the following:
"{0:N1}" -f ($colItems.sum / 1MB) + "MB"

This will give you the proper size of the folder. However, you want to concatenate it to a string, maybe something that says "Size of backup is ... MB"

To resolve this, you can simply write this data to the console as such:
Write-Host ("Size of Backup: " + "{0:N1}" -f ($colItems.sum / 1GB) + "MB")

What if you are calling a function, maybe to add something to a text file? You may need to pass this as a variable.

One more annoyance conquered. Happy scripting.

 

Related Blogs You May Be Interested In:


To leave a comment, please log in and/or register.