Script to get FIle size and ComputerName

Hi Guys

I am very new to powershell and Im trying to create a script to get a specific file and size as well as the computer name … I cant for the life of me figure out how to add the computer property . This is what i have so far

$site = Get-Content “C:\Admin Tools\space.txt”
foreach ($computer in $site){
Get-ChildItem “C:\Program Files\Cisco\AMP\nfm_url_file_map.db-wal” |
Measure-Object Length -Sum |
Select-Object @{Name=“FileSize”;Expression={$_.Sum}}
}

You were almost there! You need to create a custom object to add values not in the pipeline:

$site = Get-Content “C:\Admin Tools\space.txt”
foreach ($computer in $site){
    $size = Get-ChildItem “C:\Program Files\Cisco\AMP\nfm_url_file_map.db-wal” |
        Measure-Object Length -Sum |
        Select-Object @{Name=“FileSize”;Expression={$_.Sum}}

    [PSCustomObject]@{
        ComputerName = $computer
        FileSize = $size
    }]
}

Thank you so much! I got the computer name like i Wanted but for some reason all the file sizes are coming back at 21 mb and I checked and the are over that

$site = Get-Content “C:\Users\anthony.raimondi\Desktop\space.txt”
foreach ($computer in $site){
$size = Get-ChildItem “C:\Program Files\Cisco\AMP\nfm_url_file_map.db-wal” |
Measure-Object Length -Sum |
Select-Object @{Name=“FileSize”;Expression={$_.Sum / 1mb -as [int]}}

[PSCustomObject]@{
    ComputerName = $computer
    FileSize = $size
}

}

Oops! I wasn’t looking at the entire script… With what you have there, you are never looking at any computer except the one you are running the script from.

This script will run the Get-ChildItem commands remotely and then output locally. It assumes you have rights to all of the machines in your list, so it will throw errors if you can’t get to them for some reason.

Because I have to in my environment, I’ve also added a line to ask for credentials that have access to the filesystem on all of the remote machines.

$adminCred = Get-Credential
$site = Get-Content “C:\Admin Tools\space.txt”
foreach ($computer in $site){
    $size = Invoke-Command -ComputerName $computer -Credential $adminCred -ScriptBlock {
        Get-ChildItem “C:\Program Files\Cisco\AMP\nfm_url_file_map.db-wal” |
        Measure-Object Length -Sum
    }

    [PSCustomObject]@{
        ComputerName = $computer
        FileSize = $size.Sum
    }
}

Thank you so much again your a life saver… trying my best to learn this lol

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.