sd

When you want to be quick and nimble in your workflow, a CLI based environment saves you a lot of time. CLI for most of us is either the bash shell or other variations of the UNIX shell. The Windows operating system provides Command Prompt and the new PowerShell, but no one seems to talk about it. And now with WSL, we rarely hear of any PowerShell workflow or extensions that help us boost our productivity.

Background

In a recent turn of events at work, I had to switch from Linux to Windows. I missed the workflow terminal offered, and I had never really used PowerShell before. Cygwin and git bash is quite popular Open Source alternative for the terminal, but PowerShell came with the OS, so I went along with it. I started exploring the Powershell for my workflow, and I found it amazing. Also unlike Linux terminal, the Powershell sounded cool, it already had “Power” in its name.

But your old habits never leave you. After all these months of using the PowerShell for my workflow, I really missed a few of the commands I was accustomed to like the touch command from bash. This command is the easiest way to create new, empty files, touch {file_name}. Whereas, in PowerShell, it requires a lengthy command to do the same, New-Item -ItemType file {file_name}

Too many words to remember so I wanted something simple.

Creating a function that replicates ‘touch’

We are not trying to turn PowerShell into something that it is not. But if you are familiar with Linux terminal; then it will definitely help, the goal here is to simply streamline the PowerShell workflow.

Let’s start with PowerShell ISE which is an integrated scripting environment, installed by default.

powershell workflow getting started

And here is the preview of the environment.

powershell ise alias

Create a new file touch.ps1 and add the following script.

function touch-equi {
  param( [string[]]$files)
  Foreach ($file in $files) {
    if (Test-Path -Path $file) {
      Write-Host "$file already exists."
      continue
    }
    New-Item -ItemType file $file
  } 
}

touch-equi a.txt,b.txt,x.txt

We have created a function named touch-equi which takes an array of string as its parameter. The script loops over the provided array and first checks whether the file already exists or not and if it does, “file already exists” message will be shown; otherwise, a new file will be created.

We can simply use it as touch-equi a.txt,b.txt,x.txt where the comma-separated parameter represents an array with the name of the new files.

We can test our function by pressing F5 and we will get the following output in the ISE.

powershell workflow run

And we have successfully created three new files which you can find under then Length Name column.

Configuring PowerShell Profile

The next step is to check for PowerShell profile where we can add the function as our alias. It can be found in C:\Users\<username>\Documents\WindowsPowerShell folder. But sometimes it isn’t configured by default. You can test it via running the Test-Path $Profile command:

If it returns true then you can jump to Adding an alias to the PowerShell Profile section. else follow through it will take just a minute to create it. 

Initially, the ExecutionPolicy is set to Restricted so we have to manipulate the registry to create our profile.

Open PowerShell in Admin mode and run the following commands: 

  1. Set-ExecutionPolicy Unrestricted
  2. To be more secure
    Set-ExecutionPolicy RemoteSigned
  3. Next, we create our profile:
    New-Item -path $profile -type file -force

And we are all set! You can check again with the Test-Path $Profile command. This will create a directory called WindwosPowerShell at the following location C:{username}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

PowerShell workflow profile

Adding an alias to the PowerShell Profile

Now its time to convert our function into an alias. Open Microsoft.PowerShell_profile.ps1 file in any text editor of your choice and paste the function.

function touch-equi {
  param( [string[]]$files)
  Foreach ($file in $files) {
    if (Test-Path -Path $file) {
      Write-Host "$file already exists."
      continue
    }
    New-Item -ItemType file $file
  }    
}

Set-Alias touch-equi touch

Restart the PowerShell and that’s all. We can now use the touch alias in PowerShell.

Well, realizing the flexibility and easy implementation here’s another alias that can spawn PowerShell from a PowerShell and look how simple it is.

function open-powershell {
    Invoke-Expression 'start powershell'
}

Set-Alias pgo open-powershell

Now you can create aliases for almost everything. To explore further on PowerShell scripting, here’s a link to get you started. I hope this article has been of help to improve your PowerShell workflow.

About the Author

Sagun Karanjit is full-stack Software Engineer at Leapfrog Technology who is proficient in JavaScript, Python, and C#. He loves to solve problems in his free time.

About Leapfrog

Leapfrog is a full-stack technology services company that specializes in SaaS products, Web and Mobile Applications, and AI.
Our world-class teams have capabilities such as product design ( UI and UX), Front End and Back End Engineering, DevOps Engineering, Product Management, Data Engineering, and Machine Learning.


More awesome Augmentation awaits...

3T AI Strategy for your Business Artificial Intelligence

3T AI Strategy for your Business

When you want to be quick and nimble in your workflow, a CLI based environment saves you a lot of

Read more
Evolution of AI Artificial Intelligence

Evolution of AI

When you want to be quick and nimble in your workflow, a CLI based environment saves you a lot of

Read more
What makes a good product manager? Product Management

What makes a good product manager?

When you want to be quick and nimble in your workflow, a CLI based environment saves you a lot of

Read more