PowerShell Basic Syntax
PowerShell is not just a command-line tool; it is also a complete scripting language. Learning its basic syntax, like learning a new programming language, is an important first step to getting started.
This section will introduce the most basic syntax elements in PowerShell, including variables, comments, data types, operators, conditional statements, and loop structures, laying a solid foundation for subsequent script writing.
I. Comments
Comments in PowerShell are similar to most programming languages and are used to explain code; they are not executed.
Single-line comments start with #:
# This is a single-line comment
Write-Output "Hello, PowerShell"
Multi-line comments use <# and #> to enclose:
<#
这是多行注释
可用于文档说明
#>
II. Variables
Define a variable
In PowerShell, variables start with the $ symbol, and no type declaration is required in advance:
$name = "Alice"
$age = 25
Using variables
Write-Output "Name: $name"
You can also use string interpolation:
Write-Output "User: $($name), Age: $($age)"
III. Data Types
PowerShell is a weakly typed language, but every variable has a corresponding .NET type behind it.
| Type | Example |
|---|---|
| String | $str = "Hello" |
| Integers | $num = 123 |
| Decimal | $pi = 3.14 |
| Boolean | $isTrue = $true |
| arrays | $arr = @(1, 2, 3) |
| Hash table | $h = @{Name="Tom"; Age=30} |
You can use the .GetType() View the variable type:
$str.GetType().Name # String
IV. Operators
| Category | Example | Description |
|---|---|---|
| Arithmetic operations | + - * / % |
Common math operations |
| Comparison operations | -eq -ne -lt -gt |
Equal to, not equal to, less than, greater than |
| Logical operations | -and -or -not |
Logical operators |
| String | -like -match -replace |
Pattern matching and replacement |
| Containment operations | -in -contains |
Collection membership checks |
Example:
5 -eq 5 # True
"abc" -like "a*" # True
V. Conditional Statements
if statement
if ($age -ge 18) {
Write-Output "成年人"
} else {
Write-Output "未成年人"
}
if-elseif-else
if ($score -ge 90) {
"优秀"
} elseif ($score -ge 60) {
"及格"
} else {
"不及格"
}
VI. Loop Structures
for loop
for ($i = 1; $i -le 5; $i++) {
Write-Output "第 $i 次循环"
}
foreach Loop
$colors = @("Red", "Green", "Blue")
foreach ($color in $colors) {
Write-Output "颜色:$color"
}
while loop
$count = 0
while ($count -lt 3) {
Write-Output $count
$count++
}
VII. Function Definitions
PowerShell allows custom functions; the syntax is as follows:
function Say-Hello {
param([string]$name)
Write-Output "Hello, $name!"
}
Say-Hello -name "PowerShell"
You can also use a concise syntax:
function Square($x) { return $x * $x }
Square 5 # 输出 25
VIII. Error Handling
Use try {} catch {} block to handle statements that may fail:
try {
Get-Item "C:\NotExist.txt"
} catch {
Write-Output "找不到文件"
}
IX. Basic Script File Format
PowerShell script files use the .ps1 extension. You can create them using VS Code or Notepad:
# hello.ps1
$name = "World"
Write-Output "Hello, $name"
Run it in PowerShell:
.\hello.ps1
Note: If the script fails to execute, check the execution policy (
Get-ExecutionPolicy), and useSet-ExecutionPolicyto allow running scripts.