A Favorite PowerShell ISE Feature: Remote Text File Editing


Summary: Sean Kearney shows you how to edit files remotely in the Windows PowerShell ISE.       

Honorary Scripting Guy, Sean Kearney, is here today to show you the last of the little nuggets I love to use in the Windows PowerShell ISE—that is editing files remotely.

  Note   This is a five-part series that includes the following posts about features in the Windows PowerShell ISE:

In Windows PowerShell 5.0, there is a unique cmdlet called PSEdit. This cmdlet is also unique to the PowerShell ISE. The goal of PSEdit is to bring file editing for any PowerShell session (whether local or remote) into the ISE.

If you try to run Get-Help on the PSEdit function, you won’t see much. But it’s actually a really simple function. You can see the simple PowerShell code it uses if you access the ScriptBlock property:

(Get-Command PSEdit).ScriptBlock

Image of command output

All this does is open an array of file names that are provided to it in the current PowerShell session with a new tab for each file. Here, I use the cmdlet to open a sample script sitting in my Documents folder:

PSEdit Sample.PS1

Image of script

But how can we edit remote files in this manner? Did I forget to mention the feature called New Remote PowerShell Tab? I guess I did.

 Image of menu

When you choose this feature, it prompts for a remote computer name and User ID.

Image of menu

After I enter the correct password, I will be in a standard interactive PowerShell session within the PowerShell ISE. If you see a text file you’d like to edit, simply use the PSEdit function with the file name in question.

What is actually pretty cool is that the PowerShell ISE keeps files organized on a per session basis. What I mean by this is that as you open files under one session, they are kept under separate tabs. Here you can see that no files are opened yet in the second session:

Image of tabs

In the first session, there is at least one file opened and being edited:

Image of script

I will also mention one other really great feature that was added to the ISE—the ability to automatically recover from those times things went *poof*! This feature alone can be one of the best simple reasons to make sure you’re running something as current as Windows PowerShell 3.0, even if you don’t need workflow support in your environment.

Like in Microsoft Word, you can also get the ISE to automatically save the files you’re working on. Click Tools > Options, and then select General Settings to tweak the frequency and other settings.

Image of menu

As you can see from this menu, you can automatically save every 2 minutes and show no more than 10 recently opened files. You can choose as many as 32 files and automatically save every minute if you’d like to. However, as every person knows, if you’re working on something important, save early and save often! (You know Mr. Murphy is hiding around the corner every time!)

The PowerShell ISE has other features that make it cool for me, such as IntelliSense, but my favorite feature is the one that makes my bank account smile daily. It’s a feature of Windows, so it costs nothing to use it. The ISE can even be installed on the server. (But of course, this is not good for development scenarios because absolutely none of us would ever be caught editing our live scripts on the production server, right? …Right?)

I hope if you’ve never touched the Windows PowerShell ISE, you take this opportunity to look at it in your environment. With Windows PowerShell 5.0, it has grown to become a very powerful tool.

Of course if you’re a full blown developer, look into the PowerShell Tools for Visual Studio by Adam Driscoll to allow working with PowerShell code in Visual Studio.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy, Cloud and Datacenter Management MVP

Comments (1)

  1. Xied75 says:

    Seems doesn’t work when remote into a Nano server hosted on Azure.

    Error msg:

    Object reference not set to an instance of an object.
    At line:0 char:0

Skip to main content