locked
How to call a specific version of PowerShell from C#? RRS feed

All replies

  • powershell -version 2

    Name                           Value
    ----                           -----
    CLRVersion                     2.0.50727.5466
    BuildVersion                   6.1.7601.17514
    PSVersion                      2.0
    WSManStackVersion              2.0
    PSCompatibleVersions           {1.0, 2.0}
    SerializationVersion           1.1.0.1
    PSRemotingProtocolVersion      2.1

    powershell -version 3

    Name                           Value
    ----                           -----
    WSManStackVersion              3.0
    PSCompatibleVersions           {1.0, 2.0, 3.0}
    SerializationVersion           1.1.0.1
    BuildVersion                   6.2.9200.16481
    PSVersion                      3.0
    CLRVersion                     4.0.30319.296
    PSRemotingProtocolVersion      2.2

    Tuesday, April 23, 2013 1:43 PM
  • I know how to do it from command line.  How to call a specific version of PowerShell from C#? I have to do it programmatically.

    Dmitry

    Twitter Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog | Мой Блог

    Tuesday, April 23, 2013 6:04 PM
  • Hi,

    Hope the below similar thread could be helpful:

    call-a-specific-version-of-powershell-from-c-sharp

    http://stackoverflow.com/questions/16148305/call-a-specific-version-of-powershell-from-c-sharp

    Regards,

    Yan Li


    Cataleya Li
    TechNet Community Support

    Friday, April 26, 2013 5:27 AM
    Moderator
  • Hi Yan Li,

    Thanks for your help but I saw this thread and it didn't help me. This answer describes how to use higher version of PowerShell but I have to use lesser version (I have to run version 2.0 instead of 3.0).


    Dmitry

    Twitter Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog | Мой Блог

    Friday, April 26, 2013 10:14 AM
  • Hi Dmitry, did you ever solve this Problem? I got the same last week.
    Saturday, October 19, 2019 6:58 PM
  • Hi Dmitry, this is a workaround I am suggesting you. you can pass command or script file as below:

    To execute Command:

    scriptText="powershell.exe -version 2 -ExecutionPolicy ByPass -Command `"<Your Command>`""
    Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(scriptText);

    To execute File:

    scriptText="powershell.exe -version 2 -ExecutionPolicy ByPass -File `"<File Name>`""
    Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(scriptText);

    Monday, October 21, 2019 4:56 PM