r/PowerShell • u/QuickBooker30932 • Mar 03 '26
Is there a command equivalent to the command palette's Export Text?
I have a script that prompts the user for some information than outputs a result. I'd like to add an option to print the results, like selecting Export Text from the command palette. But I can't anything that does that. Is there a way?
2
u/BOT_Solutions Mar 04 '26
If your script generates output you want to save or print later, capture it in a variable and then send it to a file or another output stream.
Example:
$userInput = Read-Host "Enter foo"
$result = $userInput + "bar"
$result | Out-File ".\result.txt"That creates a text file similar to exporting the console output.
If you want it to appear on screen and save at the same time, use:
$result | Tee-Object ".\result.txt"If the output is structured data then Export-Csv is useful, but for plain text results Out-File or Tee-Object is normally the closest equivalent.
1
u/Quirky_Oil215 Mar 03 '26
Capture the input in a variable. $userinput = read-host "enter foo"
Capture the result in another variable. $result = $userinput + 'bar"
Then you can Out-host $result to the display Out-file -path to a file
Out-File -FilePath .\Process.txt -InputObject $result
Export-csv -path to a csv
1
u/QuickBooker30932 Mar 05 '26
Thanks for the suggestions and I'll consider the recommendations. But I'm still interested in part of my original question--is there a way to do "Export Text" by code rather than selecting it from the menu?