Here's a PowerShell script to calculate the size of the C:\Temp folder on the remote computer "RCOMP":
# Method 1: Using Invoke-Command (Recommended)
try {
$folderSize = Invoke-Command -ComputerName "RCOMP" -ScriptBlock {
$path = "C:\Temp"
if (Test-Path $path) {
$size = (Get-ChildItem -Path $path -Recurse -File | Measure-Object -Property Length -Sum).Sum
return $size
} else {
return "Path does not exist"
}
} -ErrorAction Stop
if ($folderSize -is [long]) {
$sizeInMB = [math]::Round($folderSize / 1MB, 2)
$sizeInGB = [math]::Round($folderSize / 1GB, 2)
Write-Host "Folder size on RCOMP:"
Write-Host "Bytes: $folderSize"
Write-Host "MB: $sizeInMB"
Write-Host "GB: $sizeInGB"
} else {
Write-Host "Error: $folderSize"
}
} catch {
Write-Error "Failed to connect to RCOMP or execute command: $($_.Exception.Message)"
}
Here's an alternative approach using WMI for cases where PowerShell remoting might not be enabled:
# Method 2: Using CIM/WMI (Alternative approach)
try {
$session = New-CimSession -ComputerName "RCOMP" -ErrorAction Stop
$folderSize = Get-CimInstance -CimSession $session -ClassName Win32_Directory -Filter "Name='C:\\Temp'" |
ForEach-Object {
$_.Size
}
if ($folderSize) {
$sizeInMB = [math]::Round($folderSize / 1MB, 2)
$sizeInGB = [math]::Round($folderSize / 1GB, 2)
Write-Host "Folder size on RCOMP (via WMI):"
Write-Host "Bytes: $folderSize"
Write-Host "MB: $sizeInMB"
Write-Host "GB: $sizeInGB"
} else {
Write-Host "Folder not found or has no size"
}
Remove-CimSession $session
} catch {
Write-Error "Failed to connect via WMI: $($_.Exception.Message)"
}
And here's a more comprehensive function-based approach:
function Get-RemoteFolderSize {
param(
[Parameter(Mandatory=$true)]
[string]$ComputerName,
[Parameter(Mandatory=$true)]
[string]$FolderPath,
[PSCredential]$Credential
)
try {
$scriptBlock = {
param($Path)
if (Test-Path $Path) {
$items = Get-ChildItem -Path $Path -Recurse -File -ErrorAction SilentlyContinue
$totalSize = ($items | Measure-Object -Property Length -Sum).Sum
return @{
Path = $Path
TotalSize = $totalSize
FileCount = $items.Count
Exists = $true
}
} else {
return @{
Path = $Path
TotalSize = 0
FileCount = 0
Exists = $false
}
}
}
$params = @{
ComputerName = $ComputerName
ScriptBlock = $scriptBlock
ArgumentList = $FolderPath
ErrorAction = 'Stop'
}
if ($Credential) {
$params.Credential = $Credential
}
$result = Invoke-Command @params
if ($result.Exists) {
Write-Host "Folder Analysis for $($result.Path) on $ComputerName:"
Write-Host "Files found: $($result.FileCount)"
Write-Host "Total size: $($result.TotalSize) bytes"
Write-Host "Size in MB: $([math]::Round($result.TotalSize / 1MB, 2))"
Write-Host "Size in GB: $([math]::Round($result.TotalSize / 1GB, 2))"
} else {
Write-Host "Folder $($result.Path) does not exist on $ComputerName"
}
return $result
} catch {
Write-Error "Error accessing $ComputerName : $($_.Exception.Message)"
return $null
}
}
# Usage
Get-RemoteFolderSize -ComputerName "RCOMP" -FolderPath "C:\Temp"
Prerequisites:
- PowerShell remoting must be enabled on the target computer
- You need appropriate permissions to access the remote computer
- Windows Remote Management (WinRM) service should be running
To enable PowerShell remoting on the target machine (if needed):
Enable-PSRemoting -Force
The first method using Invoke-Command is generally the most reliable and efficient approach for this task.