Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

when i open powershell and type "wsl foo" where foo is some command, manually, it works.…

  1. 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?

  2. 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:

    1. 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 RemoteSigned or Set-ExecutionPolicy Unrestricted, and confirm the change.

    2. 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.

    3. 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 like C:\path\to\wsl.exe foo. Replace C:\path\to\wsl.exe with the actual path to the wsl.exe executable on your system.

    By following these steps, you will likely be able to run the commands successfully from the .ps1 script file.