Language Basics
Learn PowerShell syntax by comparing it to C#.
PowerShell is built on the Microsoft .NET technology; you will find that most APIs in your libraries can be accessed from within the PowerShell runtime. In this section we will see similarities between the C# and PowerShell syntax.
If you're not familiar with C#, don't worry! The examples below show both languages side-by-side so you can see the patterns.
Variables
Note: Variables in PowerShell are denoted by the $ character followed by the name. You will see this through the examples below.
// C# - declare and assign
var name = "Michael";
string title = "Developer";
int count = 5;# PowerShell - assign (type is inferred)
$name = "Michael"
$title = "Developer"
$count = 5
# Optionally specify type
[string]$title = "Developer"
[int]$count = 5C# to PowerShell Translation
Use the tables below to aid in translating from C# to PowerShell. Some of the examples below are not "exact" translations, but should give you a good idea on what it would look like.
Operators are the same:
Addition:
+Subtraction:
-Multiplication:
*Division:
/Modulus:
%
Arrays
Working with Dynamic and Fixed dimensional arrays.
Using += with arrays in PowerShell is slow because it creates a new array each time. See Performance Considerations for better alternatives.
Hashtables
Working with hashtables (dictionaries).
Ordered Dictionaries
Working with dictionaries that preserve insertion order.
Switch statements:
Important: PowerShell uses different comparison operators than C#!
Comparison Operators
==
-eq
Equal to
!=
-ne
Not equal to
<
-lt
Less than
>
-gt
Greater than
<=
-le
Less than or equal
>=
-ge
Greater than or equal
Logical Operators
&&
-and
Logical AND
||
-or
Logical OR
!
-not or !
Logical NOT
Pattern Matching
Most comparisons in PowerShell are case-insensitive by default. Use operators starting with c (like -ceq) for case-sensitive comparisons.
Both work the same:
!$value- Shorter syntax-not $value- More explicit
String Interpolation
Escape Characters
Escape double quotes in string. Alternatively you can use a single quote.
Escape character: PowerShell uses backtick ` instead of backslash \
Multi-line Strings
String Operations
For Loop
ForEach Loop
While Loop
Accessing .NET Types
Common .NET Types
Creating .NET Objects
Comments
Functions and Methods
Calling Methods
Defining Functions
Error Handling
Performance Considerations
Better Collections
Creating arrays with += is slow. Use these instead:
Suppressing Output
Different methods have different performance:
Key Differences Summary
Variables
var name
$name
Equals
==
-eq
Not equals
!=
-ne
And
&&
-and
Or
||
-or
Not
!
-not or !
Escape char
\
`
String interpolation
$"text {var}"
"text $var"
Static access
DateTime.Today
[datetime]::Today
Comments
// or /* */
# or <# #>
Next Steps
Now that you understand the syntax:
Learn commands: Commands and Pipelines
Understand providers: Providers
Practice with examples: Your First Scripts
Avoid mistakes: Common Pitfalls
As you can see, the language syntax is not all that different between C# and PowerShell. Within a few minutes you might even be able to translate code from your library classes into SPE scripts!
Last updated