Being able to read C# is a huge plus when doing advanced scripting, if for no other reason than most examples in the .NET documentation are given as C# (or Visual Basic). Luckily for us, understanding C# isn’t that hard when you have worked with PowerShell a while. But one of the areas where they can differ a bit are the operators. I have created this Cheat Sheet as a tool to be able to quickly translate between C# operators and the PowerShell equivalent.
Note! This table will probably be best viewed on a larger screen. Responsive tables is not something wordpress.com does best I’m afraid.
| C# Operator | Description | Example (C#) | Example (PowerShell) |
|---|---|---|---|
| () | Specify casts, or type conversion |
double x = 1234.7; int a; // Cast double to int a = (int)x; |
$x = 1234.7 # Method one $a = [int]$x # Method two [int]$b = $x |
| () | Invoke methods or delegates |
string.Trim() |
$string.Trim() |
| . | Member access |
s.a = 6 s.b |
$s.a = 6 $s.b |
| + | Addition, concatenation |
Console.WriteLine(+5);
Console.WriteLine(5 + 5);
Console.WriteLine(5 + .5);
Console.WriteLine("5" + "5");
Console.WriteLine(5.0 + "5");
|
+5 5 + 5 5 + .5 '5' + '5' 5.0 + '5' |
| – | Subtraction |
int a = 5; Console.WriteLine(-a); Console.WriteLine(a - 1); Console.WriteLine(a - .5); |
$a = 5 -$a $a - 1 $a - .5 |
| * | Multiplication |
Console.WriteLine(5 * 2); Console.WriteLine(-.5 * .2); |
5 * 2 -.5 * .2 |
| / | Division |
Console.WriteLine(7 / 3); Console.WriteLine(-7 / 3); |
7 / 3 -7 / 3 |
| % | Remainder |
Console.WriteLine(5 % 2); Console.WriteLine(-5.2 % 2.0); |
7 % 3 -7 % 3 |
| & | logical (bitwise) AND |
// logical AND
Console.WriteLine(true & false);
Console.WriteLine(true & true);
// logical bitwise AND
Console.WriteLine("0x{0:x}", 0xf8 & 0x3f);
|
# logical AND $true -and $false $true -and $true # logical bitwise AND 0xf8 -band 0x3f |
| | | logical (bitwise) OR |
// logical OR
Console.WriteLine(true | false);
Console.WriteLine(false | false);
// logical bitwise OR
Console.WriteLine("0x{0:x}", 0xf8 | 0x3f);
|
# logical OR $true -or $false $true -or $true # logical bitwise OR 0xf8 -bor 0x3f |
| ^ | logical/bitwise exlusive-OR |
// Logical exclusive-OR
Console.WriteLine(true ^ false);
Console.WriteLine(false ^ false);
// Bitwise exclusive-OR
Console.WriteLine("{0}", Convert.ToString(0xf8 ^ 0x3f, 2));
|
# Logical exclusive-OR $true -xor $false $false -xor $false # Bitwise exclusive-OR 0xf8 -bxor 0x3f |
| ! | logical negation |
Console.WriteLine(!true); Console.WriteLine(!false); |
!$true -not $false |
| ~ | Bitwise complement |
int v = 0x111
Console.WriteLine("~0x{0:x8} = 0x{1:x8}", v, ~v);
|
$v = 0x111
"0x{0:x8} = 0x{1:x8}" -f $v, -bnot $v
|
| = | Assignment |
int i = 5; |
$i = 5 |
| < | Less than |
Console.WriteLine(1 < 1.1); Console.WriteLine(1.1 < 1.1); |
1 -lt 1.1 1.1 -lt 1.1 |
| > | Greater than |
Console.WriteLine(1.1 > 1); Console.WriteLine(1.1 > 1.1); |
1.1 -gt 1 1.1 -gt 1.1 |
| ?: | Conditional |
string classify = (input > 0) ? "positive" : "negative"; |
$classify = @{$true='positive';$false='negative'}[$myInput -gt 0]
|
| ++ | Increment |
double x; x = 1.5; Console.WriteLine(++x); x = 1.5; Console.WriteLine(x++); Console.WriteLine(x); |
$x = 1.5 "$((++$x))" $x = 1.5 "$(($x++))" $x |
| — | Decrement |
double x; x = 1.5; Console.WriteLine(--x); x = 1.5; Console.WriteLine(x--); Console.WriteLine(x); |
$x = 1.5 "$((--$x))" $x = 1.5 "$(($x--))" $x |
| && | Conditional AND |
if (Method1() && Method2())
Console.WriteLine("Both methods returned true.");
else
Console.WriteLine("At least one of the methods returned false.");
|
if (Method1 -and Method2) {
'Both methods returned true.'
}
else {
'At least on of the methods returned false.'
}
|
| || | Conditional OR |
Console.WriteLine(!(5 == 0 || 42 % 5 != 0)); |
-not ((5 -eq 0) -or ((42 % 5) -ne 0)) |
| << | Left-shift |
int i = 1;
long lg = 1;
Console.WriteLine("0x{0:x}", i << 33);
Console.WriteLine("0x{0:x}", lg << 33);
|
$i = 1 [long]$lg = 1 $i -shl 33 $lg -shl 33 |
| >> | Right-shift |
int i = -1000; Console.WriteLine(i >> 3); |
$i = -1000 $i -shr 3 |
| == | Equality |
Console.WriteLine((2 + 2) == 4); |
(2 + 2) -eq 4 |
| != | Inequality |
Console.WriteLine((2 + 2) != 4); |
(2 + 2) -ne 4 |
| <= | Less than or equal |
Console.WriteLine(1 <= 1.1); Console.WriteLine(1.1 <= 1.1); |
1 -le 1.1 1.1 -le 1.1 |
| >= | Greater than or equal |
Console.WriteLine(1.1 >= 1); Console.WriteLine(1.1 >= 1.1); |
1.1 -ge 1 1.1 -ge 1.1 |
| += | Addition assignment |
//addition int a = 5; a += 6; Console.WriteLine(a); //string concatenation string s = "Hello"; s += " world."; Console.WriteLine(s); |
#addition $a = 5 $a += 6 Write-Host $a #string concatenation $s = 'Hello' $s += ' world.' Write-Host $s |
| -= | Subtraction assignment |
int a = 5; a -= 6; Console.WriteLine(a); |
$a = 5 $a -= 6 Write-Host $a |
| *= | Binary multiplication assignment |
int a = 5; a *= 6; Console.WriteLine(a); |
$a = 5 $a *= 6 Write-Host $a |
| /= | Division assignment |
int a = 5; a /= 6; Console.WriteLine(a); double b = 5; b /= 6; Console.WriteLine(b); |
[int]$a = 5 $a /= 6 Write-Host $a [double]$b = 5 $b /= 6 Write-Host $b |
| %= | Remainder assignment |
int a = 5; a %= 3; Console.WriteLine(a); |
$a = 5 $a %= 3 Write-Host $a |
| &= | AND assignment |
int a = 0x0c;
a &= 0x06;
Console.WriteLine("0x{0:x8}", a);
bool b = true;
b &= false;
Console.WriteLine(b);
|
$a = 0x0c $a = $a -band 0x06 Write-Host $a [bool]$b = $true $b = $b -band $false Write-Host $b |
| |= | OR assignment |
int a = 0x0c;
a |= 0x06;
Console.WriteLine("0x{0:x8}", a);
bool b = true;
b |= false;
Console.WriteLine(b);
|
$a = 0x0c $a = $a -bor 0x06 Write-Host $a [bool]$b = $true $b = $b -bor $false Write-Host $b |
| ^= | Exclusive-OR assignment |
int a = 0x0c;
a ^= 0x06;
Console.WriteLine("0x{0:x8}", a);
bool b = true;
b ^= false;
Console.WriteLine(b);
|
$a = 0x0c $a = $a -bxor 0x06 Write-Host $a [bool]$b = $true $b = $b -bxor $false Write-Host $b |
| <<= | Left-shift assignment |
int a = 1000; a <<= 4; Console.WriteLine(a); |
$a = 1000 $a = $a -shl 4 Write-Host $a |
| >>= | Right-shift assignment |
int a = 1000; a >>= 4; Console.WriteLine(a); |
$a = 1000 $a = $a -shr 4 Write-Host $a |
| ?? | Null-coalescing |
int? x = null; int y = x ?? -1; |
[nullable[int]]$x = $null
if (-not($x)){$y = -1}else{$y = $x}
|