Search code examples
powershellpowershell-5.0

How to redirect output to memory instead of file in PowerShell?


I can redirect the output to a file like according to this article like this.

$PROFILE > FileOfPro.txt

Sometimes, I'd like to catch it to the memory for pasting elsewhere. There's the manual option of selecting the text and copasting (ctrlc) but it would be much more impressive and convenient to snatch the output to the memory directly.

Is it possible? I haven't found info on redirecting to memory (perhaps due to poor choice of key phrases).


Solution

  • You can copy text to clipboard using this command:

    Set-Clipboard -Value "Any thing"
    

    Or

    Set-Clipboard -Value $Variable
    

    And if though you copy text to file it is still in memory, the copied text is stored inside clipboard. Another approach is using the command CLIP, as follows.

    $PWD | clip
    

    The above, copies the entire output of the command into the clipboard. If you need the line with the actual path of the current working directory, the below will do the trick.

    $PWD.Path | clip
    

    After that, pressing ctrlv produces the line with the path.