Print

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

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.

clip_image002

In PrimalScript, I click Home and, in the Build and Run group, click Custom Tool, Copy to One Drive.

clip_image004

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.

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:

clip_image006

When you open a file and run it:

clip_image008

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:

clip_image009

When you run it, it returns this result:

clip_image011

 

Sections in a CustomMenu.inf file

There are two sections in the file:

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.

 

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.

clip_image019

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:

clip_image021

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.

If you have questions about our products, please post in our support forum.
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.