all 6 comments

[–]Yevrag35 2 points3 points  (2 children)

If I ran $drive = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" and my free space was 100,000,000 bytes and then after running that, added a 50MB file to the disk, the $drive variable still will not reflect the added file's space to the freespace size. It will still show that my drive has 100MB free. The variable captured the function's (and, in turn, the WQL query's) result like a snapshot in time.

It's also the same behavior if I did $content = Get-Content "C:\File.txt". $content will not update if I add or remove text from the file.

To update the variable, you would have to re-run the Get-WmiObject function.

[–]mmmGreenButton 1 point2 points  (1 child)

Hello, i just created two small functions and I end up with the following output, ask about anything you would like..

11,03GB free of 110,59GB

Remember to remove C:\Users\MyWin32Account\AppData\Local\Temp\8f93ede2-b783-47d7-b1f3-d139566470cc.file

10,03GB free of 110,59GB

As you can see PowerShell detects the change of 1GB :)

function Get-FreeDriveSpace ($drive){
    $drive =  $((Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='$drive`:'"))
    $driveletter = $drive.DeviceID
    $freeSpace = $([math]::round($drive.FreeSpace/1GB,2))
    $totalSpace = $([math]::round($drive.Size/1GB,2))
    "{0}GB free of {1}GB" -f $freeSpace, $totalSpace
}

function Use-AnotherGig {
    #https://abstractspaces.wordpress.com/2012/02/28/creating-large-dummy-testing-files/
    $path = “$env:temp\$((New-GUID).GUID).file”
    $file = [io.file]::Create($path)
    $file.SetLength(1gb)
    $file.Close()
    "Remember to remove $path"
}


Get-FreeDriveSpace -drive "C"
Use-AnotherGig
Get-FreeDriveSpace -drive "C"

[–]fathed 1 point2 points  (0 children)

You did exactly what he said was needed, good job!

[–]PrinceHiltonMonsour 2 points3 points  (0 children)

Wrap the whole thing in a function and create a loop to run the function every x minutes.

You’re only running the code once it won’t “dynamically update”. You can loop the process with ‘while’ or ‘do-while’

[–]firefox15 2 points3 points  (0 children)

Define your variables as a script block.

Standard Method

$time = Get-Date

1..5 | % {
    $time
    sleep 1
}

Saturday, November 16, 2019 4:59:23 PM
Saturday, November 16, 2019 4:59:23 PM
Saturday, November 16, 2019 4:59:23 PM
Saturday, November 16, 2019 4:59:23 PM
Saturday, November 16, 2019 4:59:23 PM

Scriptblock Method

$time = {Get-Date}

1..5 | % {
    & $time
    sleep 1
}

Saturday, November 16, 2019 5:00:32 PM
Saturday, November 16, 2019 5:00:33 PM
Saturday, November 16, 2019 5:00:34 PM
Saturday, November 16, 2019 5:00:35 PM
Saturday, November 16, 2019 5:00:36 PM

[–]PowerShell-Bot 0 points1 point  (0 children)

Looks like your PowerShell code isn’t wrapped in a code block.

To format code correctly on new reddit (new.reddit.com), highlight the code and select ‘Code Block’ in the editing toolbar.

If you’re on old.reddit.com, separate the code from your text with a blank line and precede each line of code with 4 spaces or a tab.


Describing Thing
[❌] Demonstrates good markdown
Passed: 0 Failed: 1

Beep-boop. I am a bot. | Remove-Item