Add Binary Path in PowerShell

Recently, I need to use makecert command in PowerShell, but makecert.exe wasn’t there by default. So, I installed a few things. But still it didn’t work. I found the executable file under C:\Program Files (x86)\Windows Kits\10\bin\x64\, but the command wasn’t recognized. Apparently, just like bash or other linux shell, it needs profile file.

The default path for the profile file is C:\Users\[username]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1. It is stored in $profile variable. You can check its value by simply typing the variable in PowerShell.

$profile

But the file does not exist by default. You can check if the file exists with typing in the following command.

test-path $profile

There’s no touch command in PowerShell. Instead, there is new-item command.

New-Item $profile

However, the directory does not exist either by default. You will see the following error message.

New-Item : Could not find a part of the path
'C:\Users\[username]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.

You can easily create a directory with mkdir. After creating the directory, then try New-Item command once again. Then, you have your own PowerShell profile. In this file, you can add the following line, then restart the PowerShell.

function Add-Path {
  <#
    .SYNOPSIS
      Adds a Directory to the Current Path
    .DESCRIPTION
      Add a directory to the current path.  This is useful for
      temporary changes to the path or, when run from your
      profile, for adjusting the path within your powershell
      prompt.
    .EXAMPLE
      Add-Path -Directory "C:\Program Files\Notepad++"
    .PARAMETER Directory
      The name of the directory to add to the current path.
  #>

  [CmdletBinding()]
  param (
    [Parameter(
      Mandatory=$True,
      ValueFromPipeline=$True,
      ValueFromPipelineByPropertyName=$True,
      HelpMessage='What directory would you like to add?')]
    [Alias('dir')]
    [string[]]$Directory
  )

  PROCESS {
    $Path = $env:PATH.Split(';')

    foreach ($dir in $Directory) {
      if ($Path -contains $dir) {
        Write-Verbose "$dir is already present in PATH"
      } else {
        if (-not (Test-Path $dir)) {
          Write-Verbose "$dir does not exist in the filesystem"
        } else {
          $Path += $dir
        }
      }
    }

    $env:PATH = [String]::Join(';', $Path)
  }
}

Add-Path -Directory "C:\Program Files (x86)\Windows Kits\10\bin\x64\"

This code is create by splunk and published in his blog

More information for PowerShell profile in MicroSoft Documentation.

PS. In my environment, running cusgom script was disabled. I had to run the following command to enable to run the script.

Set-ExecutionPolicy remotesigned
By @soundlake in
Tags : #Windows, #PowerShell, #environment,

Comments !