The PowerShell script file extension is .ps1. Save your code in a plain-text file that ends in .ps1, then run it from its folder with .\ in front of the name:
Set-Location -Path C:\psfaqs\Toolkit
Set-Content -Path .\Report.ps1 -Value "'Austin report ready'"
.\Report.ps1
Output:
Austin report ready
In this tutorial, I’ll explain each PowerShell script file extension you’ll run into, from .ps1 and .psm1 to .psd1, .ps1xml and .cdxml. I tested every example in PowerShell 7.6 and Windows PowerShell 5.1.
PowerShell file extensions at a glance
Only one of these files is a script you run directly. The rest are modules, data or configuration that PowerShell loads for you:
| Extension | What it is | How you use it |
|---|---|---|
.ps1 | Script file | Run it: .\script.ps1 |
.psm1 | Script module | Import-Module |
.psd1 | Module manifest or data file | Import-Module, Import-PowerShellDataFile |
.ps1xml | Format or type file (XML) | Update-FormatData, Update-TypeData |
.psc1 | Console file (5.1 only) | powershell.exe -PSConsoleFile |
.pssc / .psrc | Session configuration / role capability | JEA and remoting endpoints |
.cdxml | Cmdlet definition XML | Wraps CIM classes as cmdlets inside a module |
When your functions outgrow one script, you’ll create a PowerShell module and meet .psm1 and .psd1 together.
.ps1: the script file extension
PowerShell treats a .ps1 file as a script it can run. Every other extension is just a file to open. You can see that with Get-Command:
Set-Location -Path C:\psfaqs\Toolkit
Get-Command -Name .\Hello.ps1, .\Tools.psm1, .\Settings.psd1 |
Select-Object -Property Name, CommandType
Output:
Name CommandType
---- -----------
Hello.ps1 ExternalScript
Tools.psm1 Application
Settings.psd1 Application
Only Hello.ps1 is an ExternalScript. The module and data file show up as Application, which means typing their names would open them in another program instead of running them.
To pass values to a .ps1 file, see running a script from the command line with parameters. If Windows blocks it, fix the running scripts is disabled error.
.psm1: script module files
A .psm1 file holds functions that you load with Import-Module. My Tools.psm1 contains one function, Get-Greeting:
Set-Location -Path C:\psfaqs\Toolkit
Import-Module -Name .\Tools.psm1
Get-Greeting -Name 'Tom Baker'
(Get-Module -Name Tools).ModuleType
Output:
Hello, Tom Baker
Script
Import-Module loaded the function, and Get-Module reports the module type as Script. Use Get-Module to list installed modules when you need the full list.
You can’t run a .psm1 file with -File. I tried it from a batch file in both shells:
@echo off
pwsh.exe -NoLogo -NoProfile -File Tools.psm1
echo pwsh exit code: %ERRORLEVEL%
powershell.exe -NoLogo -NoProfile -File Tools.psm1
echo powershell exit code: %ERRORLEVEL%
Set-Location -Path C:\psfaqs\Toolkit
cmd.exe /c .\Ext-Test.cmd
Output:
Processing -File 'Tools.psm1' failed because the file does not have a '.ps1' extension. Specify a valid PowerShell script file name, and then try again.
pwsh exit code: 64
Processing -File 'Tools.psm1' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
powershell exit code: -196608
Both refused the file. PowerShell 7 exits with 64, while Windows PowerShell 5.1 returns -196608 and also prints its banner.
.psd1: module manifests and data files
A .psd1 file contains one hashtable. As a module manifest, it describes a module’s version, author and root module. New-ModuleManifest writes one for you:
Set-Location -Path C:\psfaqs\Toolkit
New-ModuleManifest -Path .\Tools.psd1 -RootModule Tools.psm1 -ModuleVersion 1.0.0 -Author 'Dana Reyes'
Test-ModuleManifest -Path