Add custom tools to the menu
- Details
- Written by June Blender
- Last Updated: 24 June 2016
- Created: 23 May 2016
- Hits: 13595
There are so many features in PowerShell Studio and PrimalScript that I think I can’t live without, like the Format Script button, file groups, and conditional breakpoints.
But, there are few features I’d love to have that aren’t there. They’re easy enough to add, but they’re really personal to me. Fortunately, I can add them myself.
What is a custom tool?
The Custom Tool feature in PowerShell Studio and PrimalScript lets you define commands in an INF file and add them to a menu.
For example, I have huge Scripts library on One Drive where I keep all of my scripts. When I get to a certain point in my script development, I want to copy the current script to my Scripts library without closing it or finding its path.
No problem! In PowerShell Studio, I click Tools and, in the File group, click Custom Tool, Copy to One Drive.
In PrimalScript, I click Home and, in the Build and Run group, click Custom Tool, Copy to One Drive.
As you can see, I also have options to copy to a different location, run a PowerShell Get-Acl cmdlet, run a script, open a module directory, open a Git shell, and open some very special INF files.
Which commands can I use in a custom tool?
You can run almost any command, including cmd.exe commands, Windows PowerShell commands and scripts, and commands to executable files, like robocopy.exe, netstat.exe, and git.exe.
- Cmd.exe: To run a command that’s internal to cmd.exe, precede the command with ‘cmd’ or ‘cmd.exe’.
- Executables: To run a command in an executable file, such as robocopy.exe, git.exe, or ipconfig.exe just type the command with any valid switches.
- PowerShell: To run PowerShell cmdlets, functions, or scripts, precede the command with ‘powershell’ or ‘powershell.exe’. When running scripts, be sure to include the full path to the script file. Also, verify that your local execution policy permits you to run scripts.
In the commands, in addition to using valid parameters, arguments, and switches, you can include variables for values that PowerShell Studio resolves before it sends the command to the editor.
$ProjectFolder$ | Path to the current project directory. Valid only when a project is open. |
$PowerShell$ | PowerShell version number selected on the ribbon. |
$Platform$ | Current platform (x86 or x64). |
$WinDir$ | Path to the Windows directory (e.g. C:\Windows). |
$WinSystem$ | Path to the Windows system directory (e.g. C:\Windows\System32). |
$Selection$ | Currently selected content, if any. Might contain carriage return/ linefeed characters. |
$File$ | Full path to the current file, if any. |
$FileName$ | Name of the current file (no path). |
$FileDir$ | Path to the directory of the current file (no file name). |
$Dir$ | Full path to PowerShell Studio installation directory. |
$Line$ | Current line number in the active file. |
$Col$ | Current column in the active file. |
You can also prompt for one parameter value and create a custom menu specific to a directory. I’ll show you how below, but keep these features in mind as you dream up your custom tools.
How to create a custom tool
To create a custom tool, start with a text file named CustomMenu.inf. Place the file in the ProgramData directory for PowerShell Studio or PrimalScript.
For example:
..\ProgramData\SAPIEN\PowerShell Studio 2016
..\ProgramData\SAPIEN\PrimalScript 2016
To make this simpler, I have a script, New-CustomMenu.ps1 that creates and opens the CustomMenu.inf menu for PowerShell Studio or PrimalScript.
IMPORTANT:
To make changes to the CustomMenu.inf file effective, restart PowerShell Studio or PrimalScript.
Custom Tool Syntax
The syntax of the file content is:
[Menu] 1=Command Name 1;command line 1 2=Command Name 2;command line 2 [Application] 1=Command Name 1;command line 1 2=Command Name 2;command line 2
You can add additional numbered command lines. The numbers indicate a command line. They do not determine the order of commands in the file. If the file contains multiple command lines with the same number, only the first one with each number is effective.
Examples:
For each command, enter the command name, a semi-colon (;), and the command.
For example, this line creates a ‘Run Get-Acl’ command that run the Get-Acl cmdlet on the current file ($File$), then formats all properties of the output in a list.
1=Run Get-Acl;powershell Get-Acl -Path $File$ | Format-List -Property *
Generates this menu item:
When you open a file and run it:
This line copies the current file to a Scripts directory on OneDrive.
2=Copy to OneDrive;xcopy $File$ %UserProfile%\OneDrive\Scripts\*
It generates this menu item:
When you run it, it returns this result:
Sections in a CustomMenu.inf file
There are two sections in the file:
- In the [Menu] section, add commands that run and exit, like a PowerShell cmdlet. The custom menu waits for the command to complete.
- In the [Application] section, add commands that don’t exit, like a command that displays a file or UI that you might want to keep open. The custom menu runs these commands and exits without waiting for them to complete.
For example, I have a command in the [Application] section that opens the current CustomMenu.inf file in Notepad.
[Application] 1=Open CustomMenu.inf;Notepad 'C:\ProgramData\SAPIEN\PowerShell Studio 2016\CustomMenu.inf'
Additional Syntax in a CustomMenu.inf File
You can use these features in any CustomMenu.inf file.
- Horizontal line. To add a horizontal line separator to the file, enter a single dash or hyphen on a line. For example:
3=-
- Comment. To add a comment or comment-out a line, use a semicolon (;). All text to the right of ‘;’ on that line is ignored. For example:
; Ignore this
- Prompt for input. To prompt for input, use {prompt}.For example, this ‘Copy to location’ command runs the Copy-Item cmdlet on the current file. It prompts the user for the destination path. The default message says “Enter value”.
3=Copy to location;powershell Copy-Item $File$ -Destination {prompt}
- To include a message with the prompt, use {prompt| message }For example:
3=Copy to location;powershell Copy-Item $File$ -Destination {prompt|-Destination}
- To set a default value, use {prompt|message|defaultValue}For example:
3=Copy to location;powershell Copy-Item $File$ -Destination {prompt|-Destination|$home\Dropbox\Scripts}
Folder-Specific Custom Tools
To create custom tools that appear only when the current file is stored in a particular directory, place a CustomMenu.inf file in the specified directory.
For example, I have GitHub custom commands that appear in my CustomMenu.inf only when I’m in one of my C:\GitHub subdirectories.
Here’s my default custom menu.
Here’s the CustomMenu.inf file in my GitHub clone directory. To prevent this file from being included in my repo, I create a gitignore text file and list CustomMenu.inf in that file, along with .TempPoint files.
[Menu] 1=GIT - Add To Staging;"C:\Program Files\Git\bin\Git.exe" add "$file$" 2=GIT - Commit To Local Repo;"C:\Program Files\Git\bin\Git.exe" commit $fileName$ -m "{Prompt|Enter comment|Commit from PowerShell Studio $Date$ $Time$}" 3=GIT - Commit All To Local Repo;"C:\Program Files\Git\bin\Git.exe" commit -a -m "{Prompt|Enter comment|Commit from PowerShell Studio $Date$ $Time$}" 4=- 5=GIT - Status;"C:\Program Files\Git\bin\Git.exe" status 6=- 7=GIT - Push Files To Remote Repo;"C:\Program Files\Git\bin\Git.exe" push Remote 8=GIT - Fetch From Remote Repo;"C:\Program Files\Git\bin\Git.exe" fetch Remote 9=GIT - Pull From Remote Repo;"C:\Program Files\Git\bin\Git.exe" pull Remote master
When I open a file in one of my GitHub clones on disk, my custom menu changes to:
These are just the basics of custom menus. The benefits are limited only by our creativity.
For more information about custom tools and menus, see:
June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at This email address is being protected from spambots. You need JavaScript enabled to view it. or follow her on Twitter at @juneb_get_help.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.