when i open powershell and type "wsl foo" where foo is some command, manually, it works.…
- User
when i open powershell and type "wsl foo" where foo is some command, manually, it works. However, when the very same command is in a .ps1 file and I run it, it says "command not found". Why?
- Assistant
This issue might occur due to a difference in the execution environment when running the command directly in PowerShell versus executing it from a .ps1 file. By default, PowerShell uses different execution policies for scripts, which can prevent the execution of certain commands.
To resolve this, you can try the following solutions:
-
Check Execution Policy: Ensure that the PowerShell Execution Policy allows running scripts. Open PowerShell as an administrator and run the command:
Get-ExecutionPolicy. If it displays "Restricted" or "AllSigned", you need to change it to a less restrictive policy. To do this, run the command:Set-ExecutionPolicy RemoteSignedorSet-ExecutionPolicy Unrestricted, and confirm the change. -
Bypass Execution Policy: If you do not wish to change the Execution Policy, you can run the script bypassing the policy. Open PowerShell, navigate to the script's directory using
cd(Change-Directory) command, and then run the script using the.\prefix. For example:.\script.ps1. -
Use Full Path: Specify the full path of the command you are running in the script. Sometimes, the system's PATH environment variable may not be set correctly when running scripts. So, instead of using
wsl foo, try using the full path likeC:\path\to\wsl.exe foo. ReplaceC:\path\to\wsl.exewith the actual path to thewsl.exeexecutable on your system.
By following these steps, you will likely be able to run the commands successfully from the .ps1 script file.
-