- How to Write a PowerShell Script Module
- Writing a PowerShell script module
- Create a basic PowerShell module
- Show-Calendar code example
- How to create and run a PowerShell script file on Windows 10
- How to create PowerShell script file on Windows 10
- Creating script with Visual Studio Code
- Install Visual Studio Code
- Install PowerShell extension
- Create PowerShell script with Visual Studio Code
- Creating script with Notepad
- Creating script with Integrated Scripting Environment
- How to run PowerShell script file on Windows 10
- More Windows 10 resources
- The Dell XPS 15 is our choice for best 15-inch laptop
- Halo: MCC’s live service elements make it better, not worse
- Microsoft’s Surface Duo is not ‘failing up’
- These are the best PC sticks when you’re on the move
How to Write a PowerShell Script Module
A script module is any valid PowerShell script saved in a .psm1 extension. This extension allows the PowerShell engine to use rules and module cmdlets on your file. Most of these capabilities are there to help you install your code on other systems, as well as manage scoping. You can also use a module manifest file, which describes more complex installations and solutions.
Writing a PowerShell script module
To create a script module, save a valid PowerShell script to a .psm1 file. The script and the directory where it’s stored must use the same name. For example, a script named MyPsScript.psm1 is stored in a directory named MyPsScript .
The module’s directory needs to be in a path specified in $env:PSModulePath . The module’s directory can contain any resources that are needed to run the script, and a module manifest file that describes to PowerShell how your module works.
Create a basic PowerShell module
The following steps describe how to create a PowerShell module.
Save a PowerShell script with a .psm1 extension. Use the same name for the script and the directory where the script is saved.
Saving a script with the .psm1 extension means that you can use the module cmdlets, such as Import-Module. The module cmdlets exist primarily so that you can import and export your code onto other user’s systems. The alternate solution would be to load your code on other systems and then dot-source it into active memory, which isn’t a scalable solution. For more information, see Understanding a Windows PowerShell Module. By default, when users import your .psm1 file, all functions in your script are accessible, but variables aren’t.
An example PowerShell script, entitled Show-Calendar , is available at the end of this article.
To control user access to certain functions or variables, call Export-ModuleMember at the end of your script.
The example code at the bottom of the article has only one function, which by default would be exposed. However, it’s recommended you explicitly call out which functions you wish to expose, as described in the following code:
You can restrict what’s imported using a module manifest. For more information, see Importing a PowerShell Module and How to Write a PowerShell Module Manifest.
If you have modules that your own module needs to load, you can use Import-Module , at the top of your module.
The Import-Module cmdlet imports a targeted module onto a system, and can be used at a later point in the procedure to install your own module. The sample code at the bottom of this article doesn’t use any import modules. But if it did, they would be listed at the top of the file, as shown in the following code:
To describe your module to the PowerShell Help system, you can either use standard help comments inside the file, or create an additional Help file.
The code sample at the bottom of this article includes the help information in the comments. You could also write expanded XML files that contain additional help content. For more information, see Writing Help for Windows PowerShell Modules.
If you have additional modules, XML files, or other content you want to package with your module, you can use a module manifest.
A module manifest is a file that contains the names of other modules, directory layouts, versioning numbers, author data, and other pieces of information. PowerShell uses the module manifest file to organize and deploy your solution. For more information, see How to write a PowerShell module manifest.
To install and run your module, save the module to one of the appropriate PowerShell paths, and use Import-Module .
The paths where you can install your module are located in the $env:PSModulePath global variable. For example, a common path to save a module on a system would be %SystemRoot%/users/ /Documents/PowerShell/Modules/ . Be sure to create a directory for your module that uses the same name as the script module, even if it’s only a single .psm1 file. If you didn’t save your module to one of these paths, you would have to specify the module’s location in the Import-Module command. Otherwise, PowerShell wouldn’t be able to find the module.
Starting with PowerShell 3.0, if you’ve placed your module in one of the PowerShell module paths, you don’t need to explicitly import it. Your module is automatically loaded when a user calls your function. For more information about the module path, see Importing a PowerShell Module and Modifying the PSModulePath Installation Path.
To remove a module from active service in the current PowerShell session, use Remove-Module.
Remove-Module removes a module from the current PowerShell session, but doesn’t uninstall the module or delete the module’s files.
Show-Calendar code example
The following example is a script module that contains a single function named Show-Calendar . This function displays a visual representation of a calendar. The sample contains the PowerShell Help strings for the synopsis, description, parameter values, and code. When the module is imported, the Export-ModuleMember command ensures that the Show-Calendar function is exported as a module member.
How to create and run a PowerShell script file on Windows 10
Source: Windows Central
On Windows 10, PowerShell is a command-line tool designed by Microsoft to run commands and scripts to change settings and automate tasks. In a way, it’s similar to Command Prompt. However, PowerShell is a more capable command-line interface (CLI) that offers an extensive set of tools and more flexibility and control. Also, unlike Command Prompt, PowerShell is available on Windows, macOS, and Linux.
A script is just a collection of commands saved into a text file (using the special «.ps1» extension) that PowerShell understands and executes in sequence to perform different actions.
The only caveat is that the default security protocol always blocks any script from running on a device. This means that when double-clicking a «.ps1» file on Windows 10 nothing will happen, and if you try to run the script within PowerShell, you’ll see the «cannot be loaded because running scripts is disabled on this system» error message. However, it’s not impossible to run scripts on your computer. You only need to enable the correct execution policy.
In this Windows 10 guide, we’ll walk you through the steps to successfully write and run your first script file on PowerShell using Visual Studio Code, Notepad, and the PowerShell Integrated Scripting Environment (ISE) console.
How to create PowerShell script file on Windows 10
On Windows 10, you can create PowerShell script files using virtually any text editor or the ISE console. However, the preferred option (thanks @jotaka for the heads up) to build scripts moving forward is to use the Visual Studio Code editor with the PowerShell extension.
Creating script with Visual Studio Code
Visual Studio Code — also known as VS Code — is a free and extensible cross-platform code editor that provides an environment to edit virtually any kind of programming language. And when adding the PowerShell extension, you get a fully interactive scripting editing experience, even with IntelliSense (code-completion) support.
The new experience is meant to be the new default, but the PowerShell ISE console isn’t going away. Still, the company won’t be adding any more features, and it doesn’t support PowerShell 7 or higher releases.
Install Visual Studio Code
To install Visual Basic Code on Windows 10, use these steps:
Click the Windows button to download the installer.
Source: Windows Central
Click the Next button.
Source: Windows Central
Confirm additional tasks as necessary.
Source: Windows Central
Once you complete the steps, you can proceed to install the PowerShell extension.
Install PowerShell extension
To install the PowerShell extension on VS Code, use these steps:
- Open VS Code.
- Click the Extensions tab from the left pane.
- Search for PowerShell and select the top result.
Click the Install button.
Source: Windows Central
After you complete the steps, you can start writing PowerShell scripts using Visual Studio Code.
Create PowerShell script with Visual Studio Code
To create a script with Visual Basic Code, use these steps:
- Open VS Code.
Click the File menu and select the New File option.
Source: Windows Central
Click the File menu and select the Save as option.
Source: Windows Central
Write a new, or paste the script you want to run — for example:
Write-Host «Congratulations! Your first script executed successfully»
The above script will output the phrase «Congratulations! Your first script executed successfully» on the screen.
(Optional) Click the Run button from the top-right side (or press the F5 key) to run the script.
Source: Windows Central
Creating script with Notepad
To create a PowerShell script using the Notepad editor on Windows 10, use these steps:
- Open Start.
- Search for Notepad, and click the top result to open the app.
Write a new, or paste your script, in the text file — for example:
Write-Host «Congratulations! Your first script executed successfully»
Source: Windows Central
Type a descriptive name for the script — for example, first_script.ps1.
Source: Windows Central
Creating script with Integrated Scripting Environment
Alternatively, you can use the built-in PowerShell ISE console to code your scripts on Windows 10.
The Integrated Scripting Environment is an advanced tool, but you can get started using these steps:
- Open Start.
- Search for Windows PowerShell ISE, right-click the top result, and select the Run as administrator option.
- Click on File menu.
Select the New option to create a new empty .ps1 file.
Source: Windows Central
Write a new, or paste the script you want to run — for example:
Write-Host «Congratulations! Your first script executed successfully»
Source: Windows Central
Type a name for the script – for example, first_script.ps1.
Source: Windows Central
Once you complete the steps using Notepad, Visual Studio Code, or PowerShell ISE, the script will be ready to run, but it will fail by default. This is because the default PowerShell settings are always set to block the execution of any script. (The only exception is if you run the contents of the script within Visual Studio Code or PowerShell ISE.)
How to run PowerShell script file on Windows 10
If you wish to run a script file with PowerShell, you have to change the execution policy on Windows 10.
To change the execution policy to run PowerShell scripts, use these steps:
- Open Start.
- Search for PowerShell, right-click the top result, and select the Run as administrator option.
Type the following command to allow scripts to run and press Enter:
Type A and press Enter (if applicable).
Source: Windows Central
Type the following command to run the script and press Enter:
In the above command, make sure to change «PATH\TO\SCRIPT» to the location of your script.
For example, this command runs a script stored in the Downloads folder:
Source: Windows Central
After you complete the steps, the script will run, and if it was written correctly, you should see its output without issues.
On Windows 10, PowerShell includes four execution policies, including:
- Restricted — Stops any script from running.
- RemoteSigned — Allows scripts created on the device, but scripts created on another computer won’t run unless they include a trusted publisher’s signature.
- AllSigned — All the scripts will run, but only if a trusted publisher has signed them.
- Unrestricted — Runs any script without any restrictions.
In the above steps, we use the command to allow local scripts to run on Windows 10. However, if you’re not planning to run scripts regularly, you can restore the default settings to block untrusted scripts using the same instructions outlined above, but on step No. 4, make sure to use the Set-ExecutionPolicy Restricted command.
More Windows 10 resources
For more helpful articles, coverage, and answers to common questions about Windows 10, visit the following resources:
The Dell XPS 15 is our choice for best 15-inch laptop
For a lot of people, a 15-inch laptop is a perfect size that offers enough screen for multitasking, and in a lot of cases, some extra performance from powerful hardware. We’ve rounded up the best of the best at this size.
Halo: MCC’s live service elements make it better, not worse
Halo: The Master Chief Collection is more popular than ever, but some fans don’t agree with the live service approach 343 Industries has taken with it. Here’s why those elements are, at the end of the day, great for the game and for Halo overall.
Microsoft’s Surface Duo is not ‘failing up’
Microsoft announced this week that it was expanding Surface Duo availability to nine new commercial markets. While Surface Duo is undoubtedly a work in progress, this is not a sign of a disaster. It’s also doesn’t mean that Surface Duo is selling a ton either. Instead, the reason for the expansion is a lot more straightforward.
These are the best PC sticks when you’re on the move
Instant computer — just add a screen. That’s the general idea behind the ultra-portable PC, but it can be hard to know which one you want. Relax, we have you covered!