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 = 5

C# 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.

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

C#
PowerShell
Description

==

-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

C#
PowerShell
Description

&&

-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

Use $() for expressions: "Result: $(2 + 2)" → "Result: 4"

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

Concept
C#
PowerShell

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:

  1. Learn commands: Commands and Pipelines

  2. Understand providers: Providers

  3. Practice with examples: Your First Scripts

  4. 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