Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PowerShell ISE Insert DateTime

Posted on September 15, 2010

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “PowerShell ISE Insert DateTime”

  1. Pingback: Tweets that mention PowerShell ISE Insert DateTime | The Lonely Administrator -- Topsy.com
  2. jv says:
    September 15, 2010 at 10:14 am

    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.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d