Useful PowerShell commands

Hello. Today I would like to share with you guys some useful PowerShell commands. These are more complex. Why PowerShell? Because it’s easy and much faster than some GUI operations. I will keep this post up to date, so if I will use something new and cool I add it here.


If you want to know more about PowerShell check official PowerShell website, PowerShell Github repo and awesome PowerShell Survival Guide.

Count folder size

Sometimes checking size of folder in Explorer can take ages, especially when there is a lot of small files. Don’t wait! Just use open PowerShell, provide location you want to check and use command:

"{0:N2} MB" -f ((Get-ChildItem C:\path_to_folder\ -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)

To see results in GB:

"{0:N2} GB" -f ((Get-ChildItem C:\path_to_folder\ -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)

Mass extension change

If you want to change extension for many files in folder use this:

Dir *.tiff | rename-item -newname { [io.path]::ChangeExtension($, "tif") }

In this example all files with tiff extension will be changed to tif.

Display specified number of lines

To get the first 10 lines:

Get-Content ".\test.txt" | select -First 10

To get the last 10 lines:

Get-Content ".\test.txt" | select -Last 10

Run Active Directory as different user

Use runas command to run application or other command as different user

runas /user:username@domain "mmc.exe dsa.msc"

In this case Active Directory Users and Computers snap-in. Provide your user name and domain.

To be continued…