When writing a PowerShell script or function, things like indentations, white space and case make a big difference in how easy it is to read and understand your code. Sometimes it can be helpful to have a word or sentence in all upper case so that it stands out. Here is a simple set of commands you can insert into your PowerShell ISE profile to convert selected text to all upper or lower case.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The concept is incredibly easy. I trust you know that a string object has method called ToUpper().
[cc lang="Powershell"]
PS C:\> $s="abcdefg"
PS C:\> $s.toUpper()
ABCDEFG
[/cc]
The method doesn't change the value of $s, merely returns an uppercase version. There is also a corresponding ToLower() method. In the ISE, we can use the object model to reference any selected text using
[cc lang="Powershell"]
$psise.CurrentFile.Editor.SelectedText
[/cc]
This will be a string object which means we can call the ToUpper() and toLower() methods.
[cc lang="PowerShell"]
$psise.CurrentFile.Editor.SelectedText.toUpper()
[/cc]
But what do we do with the result? We can insert it back into the file using the ISE object model.
[cc lang="PowerShell"]
$psise.currentfile.editor.insertText($psise.CurrentFile.Editor.SelectedText.toUpper())
[/cc]
Knowing this, I added two lines to my ISE profile script to append a couple of items to the AddOns menu.
[cc lang="PowerShell"]
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Convert to uppercase",
{$psise.currentfile.editor.insertText($psise.CurrentFile.Editor.SelectedText.toUpper())},"CTRL+ALT+U") | Out-Null
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Convert to lowercase",
{$psise.currentfile.editor.insertText($psise.CurrentFile.Editor.SelectedText.toLower())},"CTRL+ALT+L") | Out-Null
[/cc]
In the ISE you can run psedit $profile to edit. On my menu I now have two additional options, "Convert to uppercase" and "Convert to lowercase" with the corresponding keyboard shortcuts. If you don't want any shortcuts use $Null.
You can download these commands in text file here.
Neat… but wow me: Can you make it so that it converts aliases to full cmdlet names? Should be possible using Get-Alias, right?
You should do a whole session on customizing and tweaking the ISE. Seriously.
Hmmm…I thought the ISE Pack had such a tool but apparently not. I’ll put it on the to do list. Thanks for the suggestions.
Thank You ! Just what I needed 🙂
I think I’ve read all your post on menu add-ons for ISE. Would you care to share an exact list of what you’ve got in your add-on menu however, just in case I missed something ? 🙂
I’m fairly new to powershell and I just love how easy it is to customise & personalise ISE.
When I can find the time my plan is to package what I have so far as a module. But for now search my blog for ‘ISE’. You should find things like ISE Most Recent files, insert date time, open script folder, new comment help and new function. But perhaps I’d better package everything sooner rather than later.
Hidden ISE shortcuts:
Ctrl + Shift + U – convert to uppercase
Ctrl + U – convert to lowercase
Well isn’t that handy! I thought there were shortcuts but I couldn’t find anything on the menu. Always nice to learn something new.