Convert CSharp to PowerShell

Access the property of a variable

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class PropertyAccess
    {
        public void Method()
        {
            var timeZoneInfo = new TimeZoneInfo();
            var variable = timeZoneInfo.DisplayName;
        }
    }
}
function Method
{
	$timeZoneInfo = (New-Object -TypeName TimeZoneInfo)
	$variable = $timeZoneInfo.DisplayName
}

Array creation initializers

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            var arr = new string[] { "my", "strings" };
        }
    }
}
function Method
{
	$arr = @("my","strings")
}

Assign a constant to a variable

CSharp
PowerShell
public void Method()
{
    var variable = 1;
	var variable = "1";
}
function Method
{
	$variable = 1
	$variable = "1"
}

Cast operator

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            var myInt = 1;
            var myLong = (long)myInt;
        }
    }
}
function Method
{
	$myInt = 1
	$myLong = [long]$myInt
}

Common operators

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            var eq = 1 == 2;
            var notEq = 1 != 2;
            var or = 1 == 2 || 2 == 1;
            var and = 1 == 2 && 2 == 1;
            var gt = 1 > 2;
            var lt = 1 < 2;
            var ge = 1 >= 2;
            var le = 1 <= 2;
            var plus = 1 + 1;
            var minus = 1 - 1;
            var bor = 1 | 1;
        }
    }
}
function Method
{
	$eq = 1 -eq 2
	$notEq = 1 -ne 2
	$or = 1 -eq 2 -or 2 -eq 1
	$and = 1 -eq 2 -and 2 -eq 1
	$gt = 1 -gt 2
	$lt = 1 -lt 2
	$ge = 1 -ge 2
	$le = 1 -le 2
	$plus = 1 + 1
	$minus = 1 - 1
	$bor = 1 -bor 1
}

Converts an incomplete code block successfully.

CSharp
PowerShell
listView1.Items.Add(new ListViewItem(new string[]{"1", "content"}));
listView1.Items.Add(new ListViewItem(new string[]{"4", "content2"}));
listView1.Items.Add(new ListViewItem(new string[]{"2", "content3"}));
function Method
{
	$listView1.Items.Add((New-Object -TypeName ListViewItem -ArgumentList @("1","content")))
	$listView1.Items.Add((New-Object -TypeName ListViewItem -ArgumentList @("4","content2")))
	$listView1.Items.Add((New-Object -TypeName ListViewItem -ArgumentList @("2","content3")))
}

Create an object

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            new System.Object();
        }
    }
}
function Method
{
	(New-Object -TypeName System.Object)
}

Create an object with arugments

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            new System.Object(myVariable);
        }
    }
}
function Method
{
	(New-Object -TypeName System.Object -ArgumentList $myVariable)
}

Declare a method

CSharp
PowerShell
namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
        }
    }
}
function Method
{
}

Declare a method outside of a class or namespace

CSharp
PowerShell
void Method()
{

}
function Method
{
}

Declare a method with arguments

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method(string argument, int integer)
        {
        }
    }
}
function Method
{
	param([string]$argument, [int]$integer)
}

For loop

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            for(int i = 0; i < 100; i++)
            {
                var t = i;
            }
        }
    }
}
function Method
{
	for([int]$i = 0; $i -lt 100; $i++)
	{
		$t = $i
	}
}

Foreach loop

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method(string[] strings)
        {
            foreach(var item in strings)
            {
                var str = item;
                continue;
            }
        }
    }
}
function Method
{
	param([string[]]$strings)
	foreach ($item in $strings)
	{
		$str = $item
		continue
	}
}

If, Else If, Else

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            if (1 == 2)
            {

            }
            else if ("xyz" == (new Object()))
            {

            }
            else
            {

            }
        }
    }
}
function Method
{
	if (1 -eq 2)
	{
	}
	elseif ("xyz" -eq ((New-Object -TypeName Object)))
	{
	}
	else
	{
	}
}

Indexer property

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method(string str)
        {
            var item = str[3];
        }
    }
}
function Method
{
	param([string]$str)
	$item = $str[3]
}

Platform invoke signature

CSharp
PowerShell
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool AbortSystemShutdown(string lpMachineName);

[DllImport("credui", CharSet = CharSet.Unicode)]
public static extern CredUIReturnCodes CredUIPromptForCredentialsW(ref CREDUI_INFO creditUR,
           string targetName,
           IntPtr reserved1,
           int iError,
           StringBuilder userName,
           int maxUserName,
           StringBuilder password,
           int maxPassword,
           [MarshalAs(UnmanagedType.Bool)] ref bool pfSave,
           CREDUI_FLAGS flags);
function AbortSystemShutdown
{
	param([string]$lpMachineName)
	Add-Type -TypeDefinition '
		using System;
		using System.Runtime.InteropServices;
		public static class PInvoke {
			[DllImport("advapi32.dll", SetLastError = true)]
			public static extern bool AbortSystemShutdown(string lpMachineName);
		}
	'
	[PInvoke]::AbortSystemShutdown($lpMachineName)
}

function CredUIPromptForCredentialsW
{
	param([ref][CREDUI_INFO]$creditUR, [string]$targetName, [IntPtr]$reserved1, [int]$iError, [StringBuilder]$userName, [int]$maxUserName, [StringBuilder]$password, [int]$maxPassword, [ref][bool]$pfSave, [CREDUI_FLAGS]$flags)
	Add-Type -TypeDefinition '
		using System;
		using System.Runtime.InteropServices;
		public static class PInvoke {
			[DllImport("credui", CharSet = CharSet.Unicode)]
			public static extern CredUIReturnCodes CredUIPromptForCredentialsW(ref CREDUI_INFO creditUR,
			string targetName,
			IntPtr reserved1,
			int iError,
			StringBuilder userName,
			int maxUserName,
			StringBuilder password,
			int maxPassword,
			[MarshalAs(UnmanagedType.Bool)] ref bool pfSave,
			CREDUI_FLAGS flags);
		}
	'
	[PInvoke]::CredUIPromptForCredentialsW([ref]$creditUR, $targetName, $reserved1, $iError, $userName, $maxUserName, $password, $maxPassword, [ref]$pfSave, $flags)
}

Return statement

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public int Method()
        {
            return 1;
        }
    }
}
function Method
{
	return 1
}

Switch statement

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        void Method()
        {
            int i = 0;
            int x = 1;
            switch (i)
            {
                case 2:
                    x = 2;
                    break;
                case 3:
                    x = 3;
                    break;
                default:
                    break;
            }
        }
    }
}
function Method
{
	[int]$i = 0
	[int]$x = 1
	switch ($i)
	{
		2 {
			$x = 2
			
		}
		3 {
			$x = 3
			
		}
		default {
			
		}
	}
}

Throw statement

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Throw
    {
        public void Method()
        {
            throw new Exception("Hey");
        }
    }
}
function Method
{
	throw (New-Object -TypeName Exception -ArgumentList "Hey")
}

Try, catch, finally

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            try
            {
                var item = new object();
            }
            catch (Exception ex)
            {
                var item = new object();
            }
            catch
            {
                var item = new object();
            }
            finally
            {
                var item = new object();
            }
        }
    }
}
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)
	}
}

Using statement

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            using (AesManaged aes = new AesManaged())
            {
                aes.Padding = PaddingMode.PKCS7;
                aes.KeySize = 128;
                aes.Key = key;
                aes.IV = IV;
            }
        }

        public void Method2()
        {
            var obj = new object();
            using (obj)
            {
                aes.Padding = PaddingMode.PKCS7;
                aes.KeySize = 128;
                aes.Key = key;
                aes.IV = IV;
            }
        }
    }
}
function Method
{
	[AesManaged]$aes = $null
	try
	{
		$aes = (New-Object -TypeName AesManaged)
		$aes.Padding = $PaddingMode.PKCS7
		$aes.KeySize = 128
		$aes.Key = $key
		$aes.IV = $IV
	}
	finally
	{
		$aes.Dispose()
	}
}
function Method2
{
	$obj = (New-Object -TypeName object)
	try
	{
		$aes.Padding = $PaddingMode.PKCS7
		$aes.KeySize = 128
		$aes.Key = $key
		$aes.IV = $IV
	}
	finally
	{
		$obj.Dispose()
	}
}

While loop with break

CSharp
PowerShell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeConverter.Test.Languages.CSharp
{
    public class Class
    {
        public void Method()
        {
            while (true)
            {
                break;
            }
        }
    }
}
function Method
{
	while ($true)
	{
		break
	}
}