What is a .ps1 file?
If you want to save a series of PowerShell commands in a file so you can run them again later then you effectively creating a PowerShell script. This is simply a text file with a .ps1 extension. The file contains a series of PowerShell commands, with each command appearing on a separate line.
Why would you use a .ps1 file?
When working with SharePoint I find that most of the time to achieve something useful I’m writing several lines of PowerShell with the result of each line feeding into subsequent lines. The scripts often end up resembling source code more than traditional command line input. So using .ps1 files work well, you simply write up the commands and then execute the .ps1 file to run all the commands, make changes re-run until you end up with a script that does what you want and you’ve ironed all the bugs out.
Saving your commands to a .ps1 file also makes them portable in that you can take the file onto another computer (another SharePoint server) and execute the same .ps1 file on it. By making use of input variables, input files, and prompting for user input you can craft .ps1 files that perform common functions and you can get a lot of reuse without having to change the commands within the .ps1 file.
How do you run a .ps1 file?
You can’t just double-click on a .ps1 file in Windows Explorer to run it. The most common way to run the .ps1 file in from a PowerShell Command window. The syntax differs a bit from the old command line.
You can either specify the entire path to the file such as:
c:\temp\myscript.ps1
Or if the current directory in the PowerShell window is set to the directory containing the .ps1 file you can use the .\ shortcut to execute the script:
.\myscript.ps1
That’s it for a very basic intro to .ps1 files, happy scripting!
Great article and thanks for posting. Is it possible to pass and argument to a PS1 file? I have multiple messages and I would like to only have one file.
LikeLiked by 1 person