5

So I have a program I'd like to run in Windows PowerShell. In cmd I just type in script.cmd and it'll run the script. In PowerShell I apparently have to do ./script.cmd.

BASH behaves similarly by default. To change BASH's behavior you need to do export PATH=$PATH:. and then script.sh will work instead of just ./script.sh.

My question is... how can I make script.cmd work in PowerShell? Do I have to do something similar to export PATH=$PATH:.? I did echo $PATH in PowerShell but that didn't output anything..

1
  • note that searching in the current directly poses some security threats. That's why PowerShell went the bash way, and the new DLL search functions in Win32 APIs also removes the current folder from the search list by default
    – phuclv
    Jan 21, 2021 at 16:55

2 Answers 2

7

First of all, read get-help about_command_precedence to see how this works.

Next, type $env:path to view your current PATH.

You can append a directory to the PATH like this: $env:path += ";C:\Scripts"

You can also append the current directory to the PATH like this: $env:path += ";."

get-item env: will show you all the environment variables.

3
  • get-help about_command_precedence gives me a get-help : Get-Help could not find about_command_precedence in a help file in this session. error. Everything else worked. Thanks!
    – neubert
    May 30, 2015 at 2:14
  • 1
    That's easy to fix. Open PowerShell as admin, then use update-help to update from the internet.
    – Karan
    May 30, 2015 at 2:19
  • Update-Help failed for me with some inscrutable errors. -Force didn't help. Administrator didn't help. Update-Help -Verbose -Force -ErrorAction SilentlyContinue did work though.
    – recursive
    Mar 23, 2018 at 4:22
3

If you want to do it permanetly, this is what I do.

(And instead of adding . to the path, I'll try to add the current directory you're in right now)

You can for example, edit the registry yourself.

$oldpath = (Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$newpath = "$oldpath;$($pwd.Path)"
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
#final check
Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

Of course you need Admin Rights for that one. So let's find another way if you are not admin, and powershell gives you another way to do this, like this:

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$($pwd.Path)", "User")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .