How to create a variable from specific part of command output in powershell script

a.smith

PowerShell script runs a command to give me total bytes and total free bytes of my harddisk. I want to pick those two returned values as variables to do some calculations with but I'm not sure how to set a variable to a specific part of a command's output.

After looking through similar questions I cant apply the same logic to my situations. I have never used PowerShell before and not very familiar with Windows servers. Any recommended tutorials would be welcomed.

fsutil volume diskfree C:

Total # of free bytes : 1234567

Total # of bytes: 7654321

I.T Delinquent

I would instead use PowerShell's Get-WmiObject command and look at the Win32_LogicalDisk class. You could do something like this:

$cDrive = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceId='C:'"

That way you can get the free space and size attributes of the drive and manipulate them however you like. For example, this would show you the free space on the C drive in GB:

[Math]::Round( $cDrive.FreeSpace / 1GB)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to write a part of powershell command as a variable in python?

Capturing output from Powershell command and saving it in a variable

How to use output variable value from one powershell script to another powershell script

How to filter the output from the output of this powershell command?

To create XML output file from PowerShell script

Print a specific part of output of a command

Can I display specific part of Output from Command Prompt in Windows?

Strip a specific part from bash command output result

Save the output of a powershell command in a variable and use it in batch script?

How to create an environment variable that is the output of a command

Powershell: How to select a specific value from an output?

How to select specific string from output in Powershell

How to pass output from a PowerShell cmdlet to a script?

Pipe output from command in Git Bash to Powershell script on windows

How can I print a specific variable from a command's output using grep?

How execute specific command from shell script

how to get the standard output from command in variable

How to redirect output from command to variable?

How can I create a Bash conditional script, based on output from a command?

Call system command from perl script printing the output AND storing it into a variable

How to check in shell script if output of command contains a specific word?

How to filter a part of an output in powershell

How to call specific function in PowerShell script form Command Line?

powershell script for connecting HyperV VMM and create a VM from specific template

How to Execute Powershell Script AS Powershell 7 from Command Prompt

How to run node js script from powershell script and use the output of node js script in powershell script?

How to store the output of command "which" in a variable in a bash script?

How to capture the output of telnet command in a variable in Shell script

How to pass export variable with two find command output in shell script