YouTip LogoYouTip

Powershell Basic Syntax

PowerShell is not just a command-line tool; it is also a complete scripting language. Learning its basic syntax, just like learning a new programming language, is an important step for beginners.\n\nThis section will detail the most basic syntax elements in PowerShell, including variables, comments, data types, operators, conditional statements, loop structures, and more, laying a solid foundation for writing scripts later on.\n\n* * *\n\n## 1. Comments\n\nComments in PowerShell are similar to those in most programming languages; they are used to explain code and will not be executed.\n\nSingle-line comments start with #:\n\n# This is a single-line comment Write-Output "Hello, PowerShell"\nMulti-line comments are wrapped with :\n\n* * *\n\n## 2. Variables\n\n### Defining Variables\n\nIn PowerShell, variables start with a **`$` symbol**, and there is no need to declare the type in advance:\n\n$name = "Alice" $age = 25\n### Using Variables\n\nWrite-Output "Name: $name"\nYou can also use string interpolation:\n\nWrite-Output "User: $($name), Age: $($age)"\n\n* * *\n\n## 3. Data Types\n\nPowerShell is a weakly typed language, but variables have corresponding .NET types behind them.\n\n| Type | Example |\n| --- | --- |\n| String | `$str = "Hello"` |\n| Integer | `$num = 123` |\n| Decimal | `$pi = 3.14` |\n| Boolean | `$isTrue = $true` |\n| Array | `$arr = @(1, 2, 3)` |\n| Hash Table | `$h = @{Name="Tom"; Age=30}` |\n\nYou can use `.GetType()` to check the variable type:\n\n$str.GetType().Name # String\n\n* * *\n\n## [](#)4. Operators\n\n| Category | Example | Description |\n| --- | --- | --- |\n| Arithmetic | `+ - * / %` | Common mathematical operations |\n| Comparison | `-eq -ne -lt -gt` | Equal to, not equal to, less than, greater than |\n| Logical | `-and -or -not` | Logical operators |\n| String | `-like -match -replace` | Pattern matching and replacement |\n| Containment | `-in -contains` | Collection judgment |\n\nExamples:\n\n5 -eq 5 # True"abc" -like "a*" # True\n\n* * *\n\n## 5. Conditional Statements\n\n### if Statement\n\nif ($age -ge 18) { Write-Output "adult"} else { Write-Output "minor"}\n### if-elseif-else\n\nif ($score -ge 90) { "Excellent"} elseif ($score -ge 60) { "Pass"} else { "does not pass"}\n\n* * *\n\n## 6. Loop Structures\n\n### for Loop\n\nfor ($i = 1; $i -le 5; $i++) { Write-Output "Line $i inner loop"}\n### foreach Loop\n\n$colors = @("Red", "Green", "Blue")foreach ($color in $colors) { Write-Output "Color:$color"}\n### while Loop\n\n$count = 0while ($count -lt 3) { Write-Output $count $count++}\n\n* * *\n\n## 7. Function Definitions\n\nPowerShell allows custom functions, the syntax is as follows:\n\nfunction Say-Hello { param($name) Write-Output "Hello, $name!"}Say-Hello -name "PowerShell"\nYou can also use a concise syntax:\n\nfunction Square($x) { return $x * $x }Square 5 # Output 25\n\n* * *\n\n## 8. Error Handling\n\nUse `try {}``catch {}` blocks to handle statements that might cause errors:\n\ntry { Get-Item "C:NotExist.txt"} catch { Write-Output "File not found"}\n\n* * *\n\n## [](#)9. Script File Basic Format\n\nPowerShell script files use the `.ps1` extension. You can use VS Code or Notepad to create them:\n\n# hello.ps1 $name = "World"Write-Output "Hello, $name"\nRun in PowerShell:\n\n.hello.ps1\n> Note: If the script fails to execute, please check the execution policy (`Get-ExecutionPolicy`), and use `Set-ExecutionPolicy` to allow script execution if necessary.
← Cmdlet Process And Service ManPowershell Object Oriented Com β†’