1

Is there a good way to check if the PS version is, for example >= 5.0.0?

NOTE: I'm already using this NugetPackage: Microsoft.PowerShell.5.ReferenceAssemblies

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • 3
    @lwolf It's not a duplicate. I'm trying to do it from .NET (C#) – SuperJMN Apr 10 '19 at 18:37
  • 1
    If you're using a specific PowerShell assembly, then that's the version you're using in your application. What's on the system doesn't really matter. But the linked question will still tell you a version, if you execute that PowerShell from within C#. If you want to know the version of one of the possible executables, then you should start that executable process and check its version. Remember that Windows PowerShell and any number of PowerShell Core versions can exist side-by-side. So you have to decide what it is you want to do and know before this question can reliably be answered. – briantist Apr 10 '19 at 18:41
  • 2
    @briantist You are not correct. Reference assemblies are only for reference, them does not need to contain actual code. Application will use assemblies provided by system. – user4003407 Apr 10 '19 at 18:53
  • 2
    Thank you @PetSerAl, you are correct, my mistake. I think this can be still be answered by the linked question, but if you think it deserves its own feel free to reopen (or comment as such and I'll do it). – briantist Apr 10 '19 at 18:55
  • @briantist - i think this should be re-opened. OP is specifically asking a way to do this from C#, and the linked question is PowerShell. please re-open. – Nandun Mar 19 '21 at 16:34
  • @Nandun PowerShell is itself a .Net assembly, so it can be executed from C#, hosted directly, to find the answer, as PowerShell itself sees it (this is all in process), and if the desired version is instead that of an external executable, then the question is about starting a process from C# which I believe is also answered elsewhere. IMO it's the right duplicate, but if enough people vote to reopen it (anyone with 3000+ rep can do so) I won't weigh in or try to change that decision. – briantist Mar 19 '21 at 17:39
  • @briantist in powershell, the version is stores in $PSVersionTable variable. and it is very simple to get it in PS. in C#, it seems its no where near that simple. seems you need to traverse the registry, to get the PS version, and is completely different. Maybe its not, but i wouldn't know because this question was prematurely closed!. – Nandun Mar 19 '21 at 18:56
  • @Nandun you may want to look up how to execute PowerShell from C#, as it's quite simple. Here's one example, there are many more: https://stackoverflow.com/a/56123976/3905079 – briantist Mar 19 '21 at 19:28

0 Answers0