1

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?

Bomin
  • 1,619
  • 5
  • 24
  • 39

2 Answers2

0

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 see that it works, without actually writing the file.

D1__1
  • 925
  • 1
  • 3
  • 14
0

I'd opt for creating a temp folder on C then output the txt file to that folder.

Get-"something"| Out-File -FilePath C:\temp\temp.txt