I am trying to get the Value of the total fragmented space. I would like to to automate a check, to initiate a defrag at +10% fragmentation. I am stuck on trying to pull the total fragmented space value and assign it to variable. Thanks in advance!
I don't have anything using CSV to test this with but reading the docs it seems to just pass the command off to defrag.exe, is that correct? If so, do you just get back the output from defrag.exe? Something like this should work:
Select-String gets the relevant line from the output. We split that on the equals sign which gives us a collection containing two strings 'Total fragmented space' and 'n%'. We remove the % sign by replacing it with nothing and then trim the leading space. We then choose the second string from the collection (which has the index 1).