How to run powershell script files

Source: Internet
Author: User

You can run powershell scripts in two ways.

Before running any script file, you must first set an appropriate execution policy.

Powershell scripts, like Windows cmd, and like MS-DOS batch processing, files need to be saved as. PS1 extensions, such as myscript. PS1

 

The most common method to run a script is to call it:

PS c: \> & "C: \ belfry \ my first script. PS1"

Note: The "&" operator allows you to call a command, script, or function. For example:

PS c: \> & "C: \ Program Files \ mycommand.exe"

Ps c :\> $ runmyprog = "C: \ Program Files \ mycommand.exe"
PS c: \> & $ runmyprog

PS c: \> $ myping = Get-command-commandtype application Ping
PS c: \> & $ myping

If the path does not contain any spaces, you can ignore the quotation marks and the '&' operator.

PS c: \> C: \ belfry \ myscript. PS1

If the script is in the current path, you must use the ". \" symbol to tell powershell that the script is in the current path.

PS c: \>. \ myscript. PS1

 

Dot Sourcing

==================================

When the preceding syntax is used to execute the script, the variables and functions defined in the script disappear after the script is run.

However, if you use dot sourcing to run the script, all variables and functions defined in the script will still exist after the script is run.

Run the dot-sourcing script.

PS c: \>. "C: \ belfry \ my first script. PS1"

Run the dot-sourcing script in the current path.

PS c: \> .. \ myscript. PS1"

 

References

==================================

Run a powershell script

Http://ss64.com/ps/syntax-run.html

Using the set-executionpolicy cmdlet

Http://technet.microsoft.com/en-us/library/ee176961.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.