6

Is possible to convert PowerShell code .PS1 to C# code?

I'v been looking to it on google but I always find the contrary.

Reflector has the AddIn to convert DLL to PS Code, so I think it's possible..

6
  • 1
    Why you need this? why dont you call PS code from c#? Get a quick grasp of this blogs.msdn.com/b/kebab/archive/2014/04/28/… Commented Feb 25, 2016 at 15:04
  • 2
    Because for my company internal needs, I have to replicate the code to C#
    – hello B
    Commented Feb 25, 2016 at 15:07
  • Unfortunately, you'll probably have to do a manual port. I don't know of any tools that can convert Powershell to C#. Commented Feb 25, 2016 at 15:16
  • 2
    PowerShell is a fairly lax language and C# is quite strict, so it's pretty much a one-way conversion. Commented Feb 25, 2016 at 15:37
  • @AndreyIschencko hello B: just to be clear about Andrey's suggestion, you could end up with a C# program that is an EXE like any other C# program. However your program would just happen to invoke a PS script that was embedded in the C# source code. This wouldn't really be any different than executing SQL or XQuery statements from C#. Programs that execute SQL/XQuery from C# are still considered C# programs (by most people).
    – Χpẘ
    Commented Feb 25, 2016 at 23:09

2 Answers 2

3

You can try this Visual studio code extension : PowerShell Pro Tools for VS Code https://ironmansoftware.com/powershell-pro-tools-for-visual-studio-code/

enter image description here

Convert between C# and PowerShell code to easily take snippets of either language and convert it to the other. Quickly create P\Invoke functions in PowerShell from C# snippets found on sites like Pinvoke.net.

1
  • 7
    The op asked to convert from PS to C#, not from C# to PS
    – Steve B
    Commented Sep 20, 2021 at 11:56
1

This open source tool can do it, but the output it generates currently is not great due to the fact that PowerShell is a fairly lax language and C# is quite strict. Powershell also has a lot of commandlet wrapper functions which makes it hard to even make a tool that can really automate this.

Not the answer you're looking for? Browse other questions tagged or ask your own question.