Powershell – how to edit a file in powershell remoting session (powershell)

editingpowershellpowershell-remoting

I am connecting to another computer using powershell remoting, really nice. can do lots, but how do I edit a file?

PS C:\Users\guutlee> Enter-PSSession -ComputerName appprod

[appprod]: PS C:\Users\guutlee\Documents> cd \myapp

[appprod]: PS C:\myapp>

what can I do to open a file editor on a file on the remote machine?

[appprod]: PS C:\myapp> edit app.config

so edit "filename" just seems to hang, from powershell.exe or from powershell_ise.exe

The only thing I can think of is back out of the pssession and "start \webprod\c$\inetpub\myapp\web.config", which would open visual studio.

[appprod]: PS C:\myapp> exit

PS C:\Users\guutlee> start \agobuild\c$\myapp\app.config

PS C:\Users\guutlee> Enter-PSSession -ComputerName appprod

[appprod]: PS C:\Users\guutlee\Documents> cd \myapp

[appprod]: PS C:\myapp> myapp.exe

Of course with this I have to re-find the file, hope that the c$ share is available and accessible, and the reconnect my pssession and re-find my working directory when I want to go on. It doesn't seem very elegant.

I could maybe wrap this is a function, but having a hard time wrapping my head around that..

so how do I conveniently edit a file with a remote pssession?

EDIT

kbrimington's post got me thinking me about the -X option to ssh. probably would be an awesome thing for powershell sessions to be able to forward windowed apps back to the original windowing environment…

but still I'd be happy just to edit the file.

EDIT

tests using vi, emacs, cmd and edit

PS C:\Users\Meredith> Enter-PSSession -ComputerName appprod

[appprod]: PS C:\Users\guutlee\Documents> C:\vim\vim72\vim filename.txt

[appprod]: PS C:\Users\guutlee\Documents> C:\emacs-23.2\bin\emacs.exe -nw filename.txt

emacs.exe : emacs: standard input is not a tty

+ CategoryInfo          \: NotSpecified: (emacs: standard input is not a tty:String) [], RemoteException

+ FullyQualifiedErrorId \: NativeCommandError

[appprod]: PS C:\Users\guutlee\Documents> cmd

Microsoft Windows [Version 6.1.7600]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\guutlee\Documents>

[appprod]: PS C:\Users\guutlee\Documents> edit filename.txt

vi and edit hang (Control-C to get a prompt back)

cmd runs, producing a prompt, but immediately exits back to the powershell prompt

emacs produces the error (standard input is not a tty)

EDIT

Jered suggests pulling the file back locally to edit. I embellished his answer to copying using pssessions rather than UNCs (perhaps this is what he intended)

PS C:\Users\Meredith> Invoke-Command -Session $ps -ScriptBlock {get-content c:/inetpub/myapp/web.config} > web.config

edit web config

PS C:\Users\Meredith> get-content web.config | Invoke-Command -Session $ps -ScriptBlock {set-content c:/inetpub/myapp/web.config}

Potentially we could run the invoke-commands in either direction, local to remote or remote back to local.

Best Answer

If you are using Powershell 5, you can use command called PSEdit. It only works from ISE.

  1. So first, open PowerShell ISE
  2. Then open remote session to the remote computer using Enter-PSSession
  3. Then edit the file using PsEdit 'filename'

The remote file will be opened in a new tab in your (local) ISE window.

Actually I found this answer from the comments section of this SO question , but I think it will be helpful for others if I post it as answer here.