Introduction

If you are using any kind of electronic device, you are probably repeating certain actions multiple times per day. For example, you go to a specific website frequently every day (such as your favorite online newspaper or social networking site). In the same way, you repeat certain actions in your workplace, such as going to a admin portal or network directory several times per day.
One such action probably costs you a couple of mouse clicks or a bit of thinking (…hmm what was the name of the network directory again?). It might seem insignificant at first, but if you consider how much time you spend on this per month, then it starts to add up.
Graphical User Interfaces (GUI’s) have made computers easier to use and more accessible to the multitudes. It definitely has its advantages. However, if you compare it to traditional command line interfaces, it takes longer to achieve your objectives. With a bit of technical savvy and the right toolset, great productivity gains are within your reach.
This is where Powershell comes into play. Powershell is a windows shell with a greatly enhanced command set when compared to traditional shells like MS DOS. Powershell links seamlessly to the .NET libraries, giving you access to a very rich toolset within a command line context.
Creating custom commands within a shell is not something new, and Powershell also allows you to do this. In this post, we want to show how you can utilize the idea of creating your own custom commands in Powershell to increase your productivity.

Creating custom commands with Powershell

In your Documents directory in Windows there is a WindowsPowerShell folder. If this folder does not exist, create it. Within this folder, create a file called _Microsoft.PowerShellprofile.ps1. Inside this file you can write your own custom commands that will be incorporated into Powershell when you open it. Try it by copying the following text into this file:

function hello()
{
    write-host Hello World
}

PSDocumentsDir

Save the file and open Powershell. When you now type the command hello in the shell, the command line response string Hello World should be displayed.

PSHelloWorld-1

This kind of functionality can also be split out into modules. It is achieved by creating a modules folder inside the WindowsPowershell folder that will contain Powershell module files (with .psm extention). Each one of the Powershell module files that you create here should have its own containing folder. For example, if you create a module file called properties.psm, it should be inside ~/Documents/WindowsPowershell/properties

Tekaris base example

Now let’s look at a practical example that will make it easier for all Tekaris Employees to do some of their most frequent admin tasks. In this example, we created three Powershell Module files, namely properties.psm, run.psm, list.psm which are located in the corresponding folders as shown in the image below.

PSTekarisBaseExample

The idea is that you add frequently accessed resource uri’s inside the properties.psm file. For our Tekaris base example, this file looks as follows:

class TekarisBaseProperties : System.Object
{   
    static [string] $const\_run\_nas = "\\\\nas\\";
    static [string] $const\_run\_yammer = "https://www.yammer.com/tekaris.com";
    static [string] $const\_run\_creditCard = "…";
    static [string] $const\_run\_timeTracking = "…";
    static [string] $const\_run\_employeeDocuments = "…";
    static [string] $const\_run\_pluralsight = "https://www.pluralsight.com/";
}

Powershell also needs a way to invoke these uri’s. We achieve this by creating a run command inside the run.psm module. The command function looks as follows:

function run($env) 
{
    $obj = [TekarisBaseProperties]::New().GetType();
    Start-Process $obj.GetProperty("const\_run\_" + $env).GetValue($null); 
}

Now open Powershell again and type: run timeTracking. The time tracking webpage should open and you can log your time. Typing run pluralsight will yield a similar result. Simply use the last part of the property name in the properties.psm file and pass it as parameter to the run command.

This idea can be extended, for example, by creating a command called runAdmin. Let’s say that you have to go to four websites every day to do basic admin tasks. The runAdmin command could be configured to open these four sites.

The last part of this examples pertains to the list.psm module. This module contains one simple function that lists all the commands inside a specified module:

function listModuleCommands($moduleName)
{    
    Get-Command -module "$moduleName" | foreach { "  - $\_"} 
}

If you open Powershell and type listModuleCommands run, then it will display all the available commands in the run.psm module.

PSListModuleCommands

The run.psm module currently has 2 commands. The run command has already been explained above. The second available command is called google. This command is very simple. It takes search criteria as a parameter and then opens a webpage with google search results based on the search criteria entered. For example, if you type google Tekaris, then a webpage should open with google search results for the Tekaris keyword. If the search criteria have more than one word, then wrap it inside quotes:
google “Tekaris Software Development”

Expand your command set

You can also expand your custom command set for projects that you are working on. Let’s say you are working on a certain project that requires you to go to a specific website very often, for example http://www.welt.de. Now open your properties.psm file, and add the following line:
static [string] $const_run_welt= "https://www.welt.de/";
Now open Powershell again and type: run welt. The website http://www.welt.de should now open. You can do this for any kind of resource that you want quick access to (including network directories).

Conclusion

Powershell custom commands can be used to increase your productivity and give you quick access to resources.
The commands we used in this blog post example had the following structure:
\<verb> \<object>
e.g run timetracking

Can this basic generic command structure be expanded? For example:
\<subject> \<verb> \<object> \<indirect object>

It could also be worthwhile to implement a generic command that allows you to open multiple resources at once. Something like runMultiple. In the properties.psm file, we could specifiy an identifier that points to an array of resources. We could call this identifier _$const_run_multipleadmin, and then invoke it on the command line with runMultiple admin. This would then replace the runAdmin command as proposed above. This is also more generic as it allows us to have multiple entries that points to an array of resources.

Please contact the author to get the Tekaris base example. Simply extract TekarisBaseExample.zip into your ~/Documents/WindowsPowershell folder.

0 Kommentare

Dein Kommentar

An Diskussion beteiligen?
Hinterlasse uns Deinen Kommentar!

Schreiben Sie einen Kommentar

Ihre E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.