Join Mike Pfeiffer for an in-depth discussion in this video Working with PowerShell tabs, part of Up and Running with the PowerShell Integrated Scripting Environment.
- Writing scripts that target multiple computers can be great because it gives you the ability to do everything from a central workstation. But sometimes you need to organize your code in your sessions a little bit, and the ISE has a great built-in feature for that. It's called PowerShell tabs. Let's get started by creating a new PowerShell tab in the ISE. I'm going to go to File, come down to New PowerShell Tab. I can also invoke this by hitting Control T, but let's go ahead and click on New PowerShell Tab. And you can see now that the screen has changed. Up at the top I now have a PowerShell 2 tab. I have no script pane. And if I go back over to PowerShell 1, this is my original screen that I was looking at.
So these two environments are completely separate. These two tabs are completely isolated from each other. Let's collapse the script pane here. I'll show you what I mean. I'm going to type in $myname, and I'll set this to my name. And then I'll just make sure that the variable actually does contain that value, and it does, so that looks good. Now, if I do the same thing over here in the second tab, you'll see that there's no value, the variable is null. And that's because these two tabs are completely isolated, separate instances of PowerShell. Now, the ISE will actually let you create up to 32 tabs, and the ones that I've created so far are just working locally.
But it's also nice to use this feature to connect to remote computers. So let's go back up to File and click on New Remote PowerShell Tab. There's also a keyboard shortcut for this, which is Control + Shift + R, but let's go ahead and click on this. And when it comes up, it's asking me which computer that I want to connect to. So the key here is I need to put in a computer name that supports PowerShell remoting and has PowerShell remoting enabled and allows the traffic inbound through the firewall, and all that good stuff, and then I need to provide credentials. So it has a user name field that I could populate, and then it would prompt me for a password, if I need it.
But this development machine that I'm using is already joined to an active directory domain environment, and I'm logged in as the domain administrator. And the computer I want to connect to is dc1, this is my domain controller. So since I'm already logged in as an administrator, I don't need to provide anything else. I can just hit Connect. And when I do that, you can see that I get a different prompt. On the far left-hand side we have the computer name in brackets. And this is just standard PowerShell remoting, but if we look at the C drive, for example, you can see that that's the C drive of the domain controller. Looking at the C drive here on this computer, of course, is a little bit different.
I have an ISO folder on the root of the C drive, whereas on the domain controller I don't. So sometimes you may just use the PowerShell Tabs feature to connect to remote computers, but one of the next natural questions is how do I organize this a little bit better by maybe renaming these tabs. Well, you would think that you could right-click on them and kind of do some management that way, but you can't. However, we can programmatically modify these kinds of things through the ISE, using something called the psISE scripting object. Now, the psISE scripting object is really just an interface for us to manage the ISE from the command line or through code.
So let's take a look at what that looks like. Let's go back to the first tab. I'm going to clear the screen, and I'm going to type in psISE. And this object is always available to you in the ISE, and when you hit the period here, you can see that you get all these options. Right now we're going to work on PowerShell tabs. And I'm just going to pipe this over to select display name. And what we get back, of course, is the display names that match the tabs that we're looking at up at the top. Now, this is just a standard, generic PowerShell array that's coming back. So as you would think, we can actually use indexers to get to those array elements and manage those tabs individually.
And what that would look like is this. If we wanted to change the domain controller's tab name, we can use the second array element. Remember, arrays are zero-based in PowerShell. So PowerShell 1 is array element zero, PowerShell 2 is array element one, and then dc1 is array element two. So that's why we're using this syntax here, but then we can directly access the display name property and then change the name by changing the display name property. And watch, when we hit Enter, the dc1 tab now becomes Active Directory. We can go beyond just managing these properties, though.
We can actually perform methods on these items as well. So let's take a look at that. Let's clear the screen, we'll do psISE.PowerShellTabs.Remove. And this time I'm going to pass in a tab that I want to remove. So since I don't have a reference to it, we'll just do what we did before, psISE.PowerShellTabs on that second array item, or index two. So we pass that into the remove method and hit Enter. We get back True, basically saying that the operation was sucessful. And now if you look at the top, you'll notice that the active directory tab is now gone.
Now, there's also an option to manage the current tab that you're on. Let's take a look at that. Let's clear the screen. I'm going to do psISE.PowerShellTab, or CurrentPowerShellTab.DisplayName. And you can see that we get back PowerShell 1. So you can manage the current PowerShell tab using that object, and you can, of course, do what we did earlier to change the name. Another thing you may want to do is programmatically add a tab. So we can do something like this where we say $newtab=psISE.PowerShellTabs.create, or .Add, actually.
And then we hit Enter there. We come up with PowerShell 4, this new tab. But if we go back to the main tab that we were on, we can actually use the reference to that variable, $newtab, and change the display name and then manage that object, like we did before. Now, the last thing I want to show you is looking at the scripts that are in the current tab. So let's create a few scripts in this tab. I'm going to click on the New Script button a few times to create some scripts, and then we'll collapse the script pane, just so we can see things a little better. I'm going to use psISE.CurrentPowerShellTab.Files.
I'm going to hit Enter there, and we get back an object for every single file that's open in this tab. But if we just wanted to get the file names, we could do something like this. We could hit up arrow a couple times and recall our previous command. There's a ForEach method that we could access. And basically, within the script block, we can just basically go with $_ for the current object and get the display name for each file. So as you can see, very powerful stuff. The ISE's object model is something we'll tap into as we go through the course over time. But hopefully you've seen the value in PowerShell tabs.
Now that we've learned about tabs, let's take a look at some of the other ISE editing features that making writing PowerShell scripts a lot easier.
- Installing PowerShell ISE
- Using PowerShell tabs and profiles
- Writing, running, and saving scripts
- Working with snippets
- Adding custom menu items
- Working with add-ons