Convert PowerShell to CSharp

Cmdlets

Get-Process to Process

PowerShell
CSharp
Get-Process
Get-Process -Name "myProcess"
Get-Process -Id 1
System.Diagnostics.Process.GetProcesses();
System.Diagnostics.Process.GetProcessesByName("myProcess");
System.Diagnostics.Process.GetProcessById(1);

Out-File to File.WriteAllText

PowerShell
CSharp
function Method()
{
	Out-File -InputObject "My output test" -FilePath "supersecretfile.txt"
	Out-File "supersecretfile.txt" -InputObject "My output test" -Append
	Add-Content -Value "My output test" -Path "supersecretfile.txt"
}
void Method()
{
	File.WriteAllText("supersecretfile.txt","My output test");
	File.AppendAllText("supersecretfile.txt","My output test");
	File.AppendAllText("supersecretfile.txt","My output test");
}

Start-Process to Process.Start

PowerShell
CSharp
function Method
{
	Start-Process -FilePath "notepad.exe" -ArgumentList "myText.txt"
	Start-Process "notepad.exe" -ArgumentList "myText.txt"
}
void Method()
{
	Process process = new Process();
	ProcessStartInfo startInfo = new ProcessStartInfo();
	startInfo.FileName = "notepad.exe";
	startInfo.Arguments = "myText.txt";
	process.StartInfo = startInfo;
	process.Start();
	;
	Process process = new Process();
	ProcessStartInfo startInfo = new ProcessStartInfo();
	startInfo.FileName = "notepad.exe";
	startInfo.Arguments = "myText.txt";
	process.StartInfo = startInfo;
	process.Start();
	;
}

Write-Host to Console.WriteLine

PowerShell
CSharp
function Method
{
	Write-Host -Object "Hello, World!"
	Write-Host "Hello, World!"
}
void Method()
{
	Console.WriteLine("Hello, World!");
	Console.WriteLine("Hello, World!");
}

Loops

For loop

PowerShell
CSharp
function Method
{
	for([int]$i = 0; $i -lt 100; $i++)
	{
		[int]$t = $i
	}
}
void Method()
{
	for(int i = 0; i < 100; i++)
	{
		int t = i;
	}
}

Foreach loop

PowerShell
CSharp
function Method
{
	param([string[]]$strings)
	foreach($item in $strings)
	{
		[string]$str = $item
		continue
	}
}
void Method(String[] strings)
{
	foreach (var item in strings)
	{
		string str = item;
		continue;
	}
}

While loop with break

PowerShell
CSharp
function Method
{
	while($true)
	{
		break
	}
}
void Method()
{
	while (true)
	{
		break;
	}
}

Syntax

Access the property of a variable

PowerShell
CSharp
function Method
{
	[TimeZoneInfo]$timeZoneInfo = New-Object -TypeName TimeZoneInfo
	[string]$variable = $timeZoneInfo.DisplayName
}
void Method()
{
	TimeZoneInfo timeZoneInfo = new TimeZoneInfo();
	string variable = timeZoneInfo.DisplayName;
}

Array creation initializers

PowerShell
CSharp
function Method
{
	[string[]]$arr = @("my","strings")
}
void Method()
{
	string[] arr = new [] { "my", "strings" };
}

Assign a constant to a variable

PowerShell
CSharp
function Method
{
	[int]$variable = 1
	[string]$variable2 = "myString"
}
void Method()
{
	int variable = 1;
	string variable2 = "myString";
}

Cast operator

PowerShell
CSharp
function Method
{
	[int]$myInt = 1
	[long]$myLong = [long]$myInt
}
void Method()
{
	int myInt = 1;
	long myLong = (long)myInt;
}

Common operators

PowerShell
CSharp
function Method
{
	[bool]$eq = 1 -eq 2
	[bool]$notEq = 1 -ne 2
	[bool]$or = 1 -eq 2 -or 2 -eq 1
	[bool]$and = 1 -eq 2 -and 2 -eq 1
	[bool]$gt = 1 -gt 2
	[bool]$lt = 1 -lt 2
	[bool]$ge = 1 -ge 2
	[bool]$le = 1 -le 2
	[int]$plus = 1 + 1
	[int]$minus = 1 - 1
	[int]$bor = 1 -bor 1
}
void Method()
{
	bool eq = 1 == 2;
	bool notEq = 1 != 2;
	bool or = 1 == 2 || 2 == 1;
	bool and = 1 == 2 && 2 == 1;
	bool gt = 1 > 2;
	bool lt = 1 < 2;
	bool ge = 1 >= 2;
	bool le = 1 <= 2;
	int plus = 1 + 1;
	int minus = 1 - 1;
	int bor = 1 | 1;
}

Create an object

PowerShell
CSharp
function Method
{
	New-Object -TypeName System.Object
	New-Object System.Object
}
void Method()
{
	new System.Object();
	new System.Object();
}

Create an object with arugments

PowerShell
CSharp
function Method
{
	New-Object -TypeName System.Object -ArgumentList $myVariable
}
void Method()
{
	new System.Object(myVariable);
}

Declare a method

PowerShell
CSharp
function Method
{
}
void Method()
{
}

Declare a method with arguments

PowerShell
CSharp
function Method
{
	param([string]$argument, [int]$integer)
}
void Method(String argument, Int32 integer)
{
}

If, Else If, Else

PowerShell
CSharp
function Method
{
	if (1 -eq 2)
	{
		[int]$variable = 1;
	}
	elseif ("xyz" -eq (New-Object -TypeName Object))
	{
		[int]$variable = 2;
	}
	else
	{
		[int]$variable = 3;
	}
}
void Method()
{
	if (1 == 2)
	{
		int variable = 1;
	}
	else if ("xyz" == (new Object()))
	{
		int variable = 2;
	}
	else
	{
		int variable = 3;
	}
}

Indexer property

PowerShell
CSharp
function Method
{
	param([string]$str)
	[string]$item = $str[3]
}
void Method(String str)
{
	string item = str[3];
}

Return statement

PowerShell
CSharp
function Method
{
	return 1
}
void Method()
{
	return 1;
}

Static method

PowerShell
CSharp
function Method()
{
	[Guid]::New()
}
void Method()
{
	Guid.New();
}

Switch statement

PowerShell
CSharp
function Method
{
	[int]$i = 0
	[int]$x = 1
	switch ($i)
	{
		2 { $x = 2 }
		3 { $x = 3 }
		default { }
	}
}
void Method()
{
	int i = 0;
	int x = 1;
	switch (i)
	{
		case 2:
			x = 2;
			break;
		case 3:
			x = 3;
			break;
		default:
			break;
	}
}

Throw statement

PowerShell
CSharp
function Method
{
	throw (New-Object -TypeName Exception -ArgumentList "Hey")
}
void Method()
{
	throw (new Exception("Hey"));
}

Try, catch, finally

PowerShell
CSharp
function Method
{
	try
	{
		$item = New-Object -TypeName object
	}
	catch [Exception]
	{
		$item = New-Object -TypeName object
	}
	catch
	{
		$item = New-Object -TypeName object
	}
	finally
	{
		$item = New-Object -TypeName object
	}
}
void Method()
{
	try
	{
		item = new object();
	}
	catch (Exception)
	{
		item = new object();
	}
	catch
	{
		item = new object();
	}
	finally
	{
		item = new object();
	}
}