Home > Enterprise >  How to use Powershell output in a pipeline
How to use Powershell output in a pipeline

Time:02-02

My purpose is to write some string into a file, and the file path is something like %SystemDrive%\temp.txt. The pipeline looks like

 Write-Output "test" |  Out-File -FilePath %SystemDrive%\temp.txt

I can get %SystemDrive% by (Get-ChildItem -Path Env:\SystemDrive).Value, but how can I put it into the pipeline?

CodePudding user response:

You could use Join-Path wrapped in parenthesis to set the FilePath for Out-File.

Write-Output "test" | Out-File -FilePath (Join-Path -Path (Get-ChildItem -Path Env:\SystemDrive).Value -ChildPath "\temp.txt")

The path that this yields on my system is:

C:\temp.txt

You can add the -WhatIf switch to the end of the command to that it works without actually writing the file.

  • Related