How to Run PowerShell Script (Mac/Windows/Linux)


If you want to run a Powershell script (.ps1) on your device, you can do that in the following ways depending upon the OS that you are on.


On Mac (macOS)

    1. Open Terminal (type 'Terminal' in Spotlight search and press enter)
    2. Now Type:
      pwsh ./path/to/your/powershell/script.ps1
    3. Note: You should have Powershell for Mac installed on your Mac. If not follow the link - https://code2care.org/howto/install-powershell-on-macos

    4. The script will get executed.
    5. Run PowerShell Script Example

On Windows

    1. Open PowerShell (type 'PowerShell' in the Start menu and press Enter)
    2. Now, navigate to the directory containing your PowerShell script using the `cd` (Change Directory) command:
      cd C:\path\to\your\script\directory
    3. Execute your PowerShell script using the `.\` prefix to specify the current directory:
      .\powershell-script.ps1
    4. The script will get executed.

On Linux (Ubuntu, Debian, CentOS, etc.)

    1. Open a terminal (You can find the terminal application in your applications menu)
    2. Navigate to the directory containing your PowerShell script using the cd (Change Directory) command:
      cd /path/to/your/script/directory
    3. Make sure you have PowerShell installed on your Linux distribution. If not, follow the official Microsoft documentation for installation: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux
    4. Execute your PowerShell script using the pwsh command:
      pwsh ./powershell-script.ps1
    5. The script will get executed.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap