Thursday, August 29, 2019

How to find the size of a folder using PowerShell?



You can use the  Get-ChildItem to get the list of files and directories.

Mode                LastWriteTime         Length Name                                  
----                -------------         ------ ----                                  
d-----       2019-08-19  10:42 AM                APPDATA output                        
d-----       2019-08-12   3:18 PM                AWS                                   
-a----       2019-08-16  12:35 PM          15806 AppDataSize.csv                       
-a----       2019-08-28   3:39 PM          15358 Appwiz.txt                            


The Length object gives the size of the file, however that to get the size of the folder, we can use the  -Recurse to dive deep into the folder to get the details

PowerShell Code: 
Get-ChildItem -Path 'C:\Temp\APPDATA output' 

Mode                LastWriteTime         Length Name                                  
----                -------------         ------ ----                                  
-a----       2019-08-16  12:35 PM          15806 AppDataSize.csv                        
-a----       2019-08-16   1:12 PM           3909 AppDataSize2.csv                      
-a----       2019-08-16   3:33 PM           2744 AppDataSize2_User2.csv                
-a----       2019-08-16   5:18 PM          10737 AppDataSize_User.csv                   
-a----       2019-08-15  10:42 AM           2745 finalappdata.ps1                      
-a----       2019-08-16   1:12 PM          34616 output.txt 

PowerShell Code:
Get-ChildItem -Path 'C:\Temp\APPDATA output'| Measure-Object -Property Length -sum

Count    : 6
Average  :
Sum      : 70557
Maximum  :
Minimum  :
Property : Length


PowerShell Code: 
#Get the folder size and store it in the $Folder Variable
$Folder=Get-ChildItem -Path 'C:\Temp\APPDATA output'| Measure-Object -Property Length -sum | Select sum
#Converting them to a decimal number with the Size in MB
$FolderSize="{0:N2}" -f ($Folder.Sum / 1MB) + " MB"
Echo "Folder Size = $FolderSize"

Folder Size = 0.07 MB



To get the size of a folder in remote location we can use the same command and give the path of the location

PowerShell Code: 
#Get the folder size and store it in the $Folder Variable
$Folder=Get-ChildItem -Path '\\server1\Temp\APPDATA output'| Measure-Object -Property Length -sum | Select sum
#Converting them to a decimal number with the Size in MB
$FolderSize="{0:N2}" -f ($Folder.Sum / 1MB) + " MB"
Echo "Folder Size = $FolderSize"





Wednesday, August 28, 2019


How to get the list of hard disk on the computer using PowerShell?

To get the list of drives we can use the wmiobject to get the details

PowerShell Code:
Get-WmiObject -Class win32_logicaldisk


DeviceID     : C:
DriveType    : 3
ProviderName :
FreeSpace    : 43298725888
Size         : 255073447936
VolumeName   : OSDisk


We can select the information that we want to display  by using the Select-object
PowerShell Code:
Get-WmiObject -Class win32_logicaldisk | select DeviceID,FreeSpace, Size


DeviceID     FreeSpace          Size
--------     ---------          ----
C:         43306131456  255073447936
H:       1010439315456 2582761172992
X:       2223348092928 3221088104448
Y:       1535164190720 3221088104448
Z:         17846333440   85896196096

We can modify the result to display it in MB or GB along with the decimal digit
PowerShell Code:
$Drive = Get-WmiObject -Class win32_logicaldisk | select DeviceID, FreeSpace, Size
echo "Drive Letter `t Free_Space `t Full_Space"
echo "==========================================="
foreach($Drive in $Drive)
{
$DriveName=$Drive.DeviceID
$DriveFreeSpace="{0:N2}" -f ($Drive.FreeSpace / 1GB) + " GB"
$DriveSize="{0:N2}" -f ($Drive.Size / 1GB) + " GB"

echo "$DriveName `t`t`t`t $DriveFreeSpace`t`t$DriveSize" |Format-Table
}


Drive Letter  Free_Space  Full_Space
===========================================
C:            40.33 GB            237.56 GB
H:            940.99 GB           2,405.38 GB
X:            2,070.65 GB         2,999.87 GB
Y:            1,429.73 GB         2,999.87 GB
Z:            16.62 GB            80.00 GB

You could use the invoke command to get the same details from the remote computer

Invoke-Command -ComputerName Client7-1 -ScriptBlock {

$Drive = Get-WmiObject -Class win32_logicaldisk | select DeviceID, FreeSpace, Size
echo "Drive Letter `t Free_Space `t Full_Space"
echo "==========================================="
foreach($Drive in $Drive)
{
$DriveName=$Drive.DeviceID
$DriveFreeSpace="{0:N2}" -f ($Drive.FreeSpace / 1GB) + " GB"
$DriveSize="{0:N2}" -f ($Drive.Size / 1GB) + " GB"

echo "$DriveName `t`t`t`t $DriveFreeSpace`t`t$DriveSize" |Format-Table
}
}

==============================================================

Clean and Alternative code to view the Hard disk and the free space 

Get-WmiObject -class Win32_LogicalDisk -computername localhost |
Sort-Object -property DeviceID | Format-Table -property @{label ='Drive';expression={$_.DeviceID}}, @{label='FreeSpace(MB)';expression={$_.FreeSpace / 1MB -as [int]}}, @{label='Size(GB)';expression={$_.Size / 1GB -as [int]}}, @{label='%Free';expression={$_.FreeSpace / $_.Size * 100 -as [int]}}



This Code
@{label='FreeSpace(MB)';expression={$_.FreeSpace / 1MB -as [int]}}”
Will give the column name as for the freespace value and it converts to MB




Drive FreeSpace(MB) Size(GB) %Free
----- ------------- -------- -----
C:            39898      238    16
H:           953496     2405    39
X:          2116792     3000    69
Y:          1459925     3000    48
Z:            16941       80    21


How to check the list of programs installed on a Windows PC using PowerShell?

How to get the list of software installed on the computer using PowerShell?

All the programs that are installed in the computer are available under the following registry Hive.

HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

We could print the Subkeys from there

PowerShell Code:
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*

AuthorizedCDFPrefix :
Comments            : DELL Command Configure 3.3 (AP12538)
Contact             : Customer Support Department
DisplayVersion      : 3.3.0.314
HelpLink            : http://support.dell.com
HelpTelephone       : 1-877-717-3355
InstallDate         : 20190628
InstallLocation     : C:\Program Files (x86)\Dell\
InstallSource       : C:\WINDOWS\ccmcache\h\
ModifyPath          : MsiExec.exe
Publisher           : Dell Inc.
Readme              :
Size                :
EstimatedSize       : 59175
UninstallString     : MsiExec.exe /
URLInfoAbout        : http://www.dell.com
URLUpdateInfo       : http://support.dell.com
VersionMajor        : 3
VersionMinor        : 3
WindowsInstaller    : 1

Version             : 50528256 

You can select the properties that you want to use by using the 'Select-Object' command


PowerShell Code:
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate  


DisplayName                                                             DisplayVersion   Publisher                         InstallDate
-----------                                                             --------------   ---------                         -----------
Adaptiva Client 5.5 Build 677                                           5.5.677.1        Adaptive Protocols Inc            20190628  

How to suspend an script activity Temporarily ?

There are situations where the script execution needs to be suspended or paused for few milliseconds or seconds. PowerShell Syntax St...