YouTip LogoYouTip

Powershell Script

* * *\n\n## [](#)Introduction\n\nPowerShell is a powerful command-line scripting language that can not only run commands interactively, but also combine commands into script files to accomplish complex automation tasks.\n\nThis article will guide you step by step to master the basic skills of PowerShell script development, including:\n\n* How to create and run `.ps1` scripts\n* How to define and use functions\n* How to develop modularly and reuse your script logic\n\n* * *\n\n## I. PowerShell Script Basics\n\n### Creating Script Files (.ps1)\n\nPowerShell script files are plain text files with the **`.ps1`** extension, containing a set of PowerShell commands to be executed.\n\n#### Example: hello.ps1\n\n# hello.ps1Write-Output "Hello from PowerShell script!"\nYou can create `.ps1` files with any text editor (VS Code recommended).\n\n### Execution Policy\n\nFor security reasons, Windows **does not allow script execution by default**, so you need to set the execution policy.\n\nView current policy:\n\nGet-ExecutionPolicy\nTemporarily set to allow local scripts to run:\n\nSet-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned\n> RemoteSigned: Local scripts can run without signing, but downloaded scripts must be signed by a trusted publisher.\n\n### Running Scripts\n\nRun from command line:\n\n.hello.ps1\nRun with specified PowerShell executable:\n\npowershell.exe -File .hello.ps1 pwsh.exe -File .hello.ps1 # PowerShell 7+\nRight-click "Run with PowerShell" (requires policy configuration)\n\n### Parameter Passing and Handling\n\nYou can define how to receive external parameters in scripts:\n\n#### Example: Script with parameters greet.ps1\n\nparam ( $name, $age )Write-Output "Hello,$name,Your age is $age years old."\nRun with parameters:\n\n.greet.ps1 -name "Xiaoming" -age 18\nAlso supports default values and parameter validation:\n\nparam ( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] $City = "Beijing")\n\n* * *\n\n## II. Using Functions\n\n### Function Definition and Calling\n\nfunction Say-Hello { Write-Output "Hello from function"}Say-Hello\n### Parameter Declaration and Validation\n\nFunctions can also take parameters, defined using the `param` block:\n\nfunction Add-Numbers { param ( $a, $b ) return $a + $b }Add-Numbers -a 3 -b 5 # Output 8\nPositional parameters can also be used:\n\nfunction Add { param ($x, $y) $x + $y }Add 10 20\n### Return Value Handling\n\nPowerShell functions return all output by default (including `Write-Output`).\n\nUsing `return` is recommended for explicit returns:\n\nfunction Get-Square { param ($num) return $num * $num } $result = Get-Square -num 6Write-Output $result\n### Function Scope\n\nVariables defined inside functions are local by default:\n\nfunction Demo-Scope { $message = "Hello"}Demo-ScopeWrite-Output $message # Cannot access\nTo access variables from outside, use scope prefixes:\n\n$global:x = 5\n\n* * *\n\n## III. Modular Development\n\n### What is a PowerShell Module\n\nA module is a collection of functions, scripts, and resources used to organize code and facilitate reuse.\n\nCommon module types:\n\n* Script modules (`.psm1`)\n* Manifest modules (`.psd1`)\n* Binary modules (.dll)\n\n### Importing and Using Modules\n\nUse `Import-Module` to import modules:\n\nImport-Module MyModule.psm1\nView loaded modules:\n\nGet-Module\nCall functions from the module:\n\nSay-Hello\n### PowerShell Gallery Introduction\n\n(https://www.powershellgallery.com/) is the official PowerShell module repository where you can download and install thousands of modules.\n\nExample of installing a module:\n\nInstall-Module -Name PSReadLine\nUpdate a module:\n\nUpdate-Module PSReadLine\n### Creating a Simple Custom Module\n\nCreate a new MyModule.psm1 file and add functions:\n\n# MyModule.psm1function Greet-User { param ($name) Write-Output "Welcome you,$name"}\nImport and call in a script:\n\nImport-Module .MyModule.psm1 Greet-User -name "Xiaohong"\n> Tip: You can place module files in paths listed in `$env:PSModulePath` for automatic discovery.\n\n* * *\n\n## IV. Summary\n\n| Content | Description |\n| --- | --- |\n| `.ps1` file | PowerShell script extension, stores commands |\n| `param` | Used to define script/function parameters |\n| `Set-ExecutionPolicy` | Controls whether scripts are allowed to run |\n| Functions | Encapsulate reusable code, improve structural clarity |\n| Modules (`.psm1`) | Organize multiple functions, enable portability |\n| PowerShell Gallery | Official module repository, download third-party modules |
← Windows Wsl LinuxPowershell Variables And Scope β†’