I still don't leverage the PowerShell Integrated Script Editor (ISE) as much as I should. But after reading a few recent entries from The Scripting Guys on inserting help and headers into a script, I thought I'd dig in a little more. I've a few things to share but today I want to show you what I did to insert a datetime into a script.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
I'm assuming like many of you, Notepad is still a trusty tool. One of my favorite features is the F5 shortcut which will insert the current date and time. Very often when working on scripts in the ISE, I wanted to insert a current date and time and longed for that shortcut. Now I have it and it is really pretty simple. At the command prompt in the ISE all you need is this expression:
[cc lang="Powershell"]
$psise.CurrentFile.Editor.InsertText($(get-date))
[/cc]
That's it. The current date and time will be inserted in the current file at the cursor. Of course, I don't want to always have to type that, so in my ISE profile script, Microsoft.PowerShellISE_profile.ps1, which is in your Windows PowerShell folder with your other profiles, I added some code to add this command to the menu with a keyboard shortcut. If you don't have the profile script, you'll have to create it.
In my ISE profile script I added the following line.
[cc lang="PowerShell"]
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Insert Datetime",{$psise.CurrentFile.Editor.InsertText($(get-date))},"ALT+F5") | out-Null
[/cc]
What I've done is invoke the Add() method from the Submenus object. This method takes 3 parameters:; The text that will display under the Add-Ons menu, a script block that will execute, and a keyboard shortcut. I pipe it to Out-Null purely for cosmetic reasons. Otherwise, I see the command in the output window whenever I run it.
Now, whenever I press ALT+F5, I get the date and time. F5 is already reserved for executing the script so I had to accept a slight substitution. By the way, I also load the functions from The Scripting Guys in my ISE profile and add menu items for them as well; one with a keyboard shortcut and one without.
[cc lang="PowerShell"]
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Add Header",{Add-HeaderToScript},$null) | out-Null
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Add Help",{Add-Help},"ALT+H") | Out-Null
[/cc]
I have a little more to share on this topic but that will be for another day.
FOr nonISE users teh easy way to edit your ISE profile is to type ‘notepad $profile’ from ISE. notepad will create or edit the file.