Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Set Environment Variable based on WMI data

$
0
0

Basic Use Case for what I need to do:

I need to query the local drives and find the drive with the most free space and then set a system environment variable for another outside process to read.

I've got the first part working but I can't figure out the second part or creating the environment variable.

PS > Get-WmiObject win32_logicaldisk -Filter DriveType=3 | Sort-Object FreeSpace -Descending | Select-Object -ExpandProperty DeviceID -First 1

Result is

D:

 

 

I've then tried using set-item to create the value and this is where I'm having trouble.  I've also tried removing the -ExpandProperty to take it back to an object vs a string.

PS > Get-WmiObject win32_logicaldisk -Filter DriveType=3 | Sort-Object FreeSpace -Descending | Select-Object -ExpandProperty DeviceID -First 1 | Set-Item -Path Env:UPDATEDRIVE -Value $PSItem

Set-Item : The input object cannot be bound to any parameters for the command either because the command does not takepipeline input or the input and its properties do not match any of the parameters that take pipeline input.

At line:1 char:149

+ Get-WmiObject win32_logicaldisk -Filter DriveType=3 | Sort-Object FreeSpace -Descending | Select-Object -ExpandProperty DeviceID -First 1 | Set-Item <<<<  -Path Env:UPDATEDRIVE -Value $PSItem

    + CategoryInfo          : InvalidArgument: (D::PSObject) [Set-Item], ParameterBindingException

    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.SetItemCommand

 

 

Thanks in advance

--Todd

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles