Skip to main content
ubuntuask.com

Back to all posts

How to Set Variable In Powershell Script From C#?

Published on
4 min read
How to Set Variable In Powershell Script From C#? image

Best PowerShell Automation Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

BUY & SAVE
$27.00 $59.99
Save 55%
Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts
4 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
5 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.52 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
6 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$34.99
Learn Windows PowerShell in a Month of Lunches
7 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
8 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

BUY & SAVE
$28.99
PowerShell for Sysadmins: Workflow Automation Made Easy
9 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$31.99 $44.99
Save 29%
Learn Windows PowerShell in a Month of Lunches
10 Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis

Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis

BUY & SAVE
$22.26 $48.99
Save 55%
Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis
+
ONE MORE?

In order to set a variable in a PowerShell script from C#, you can use the Runspace class provided by the System.Management.Automation namespace. First, create an instance of the Runspace class and open it. Next, create a Pipeline object and add commands to it using the Add method. Finally, invoke the Invoke method on the pipeline to execute the commands, including setting the variable with the desired value. Remember to close the runspace once you are done with it to release resources.

How to pass an array as a variable from C# to a PowerShell script?

To pass an array as a variable from C# to a PowerShell script, you can use the AddParameter method of the PowerShell class to pass the array as a parameter to the PowerShell script.

Here is an example of how you can pass an array as a variable from C# to a PowerShell script:

  1. Create a PowerShell script that accepts an array parameter:

Param( [String[]]$MyArray )

Use the array in the script

foreach ($item in $MyArray) { Write-Output $item }

  1. In your C# code, you can use the following code to pass an array as a variable to the PowerShell script:

using System.Management.Automation;

// Create an instance of the PowerShell class PowerShell ps = PowerShell.Create();

// Define the array string[] myArray = { "Item1", "Item2", "Item3" };

// Add the array as a parameter to the PowerShell script ps.AddScript(@"C:\path\to\your\script.ps1") .AddParameter("MyArray", myArray);

// Execute the script ps.Invoke();

In the above code, MyArray is the parameter name in the PowerShell script and myArray is the array defined in C#. You can replace the script path with the actual path to your PowerShell script.

By using the AddParameter method of the PowerShell class, you can pass the array as a variable from C# to a PowerShell script.

How to pass variable from C# to PowerShell script?

To pass a variable from C# to a PowerShell script, you can use the AddParameter method of the Command class in the System.Management.Automation namespace. Here's an example of how you can do this:

  1. Create a PowerShell script with a parameter that will receive the variable value:

param( [string]$myVariable )

Use the value of the variable.

Write-Output "The value of myVariable is: $myVariable"

  1. In your C# code, use the following code snippet to pass the variable value to the PowerShell script:

using System; using System.Management.Automation;

class Program { static void Main() { using (PowerShell ps = PowerShell.Create()) { ps.AddScript(@"C:\path\to\your\script.ps1") .AddParameter("myVariable", "Hello from C#") .Invoke(); } } }

In the above code snippet, replace "Hello from C#" with the variable value you want to pass to the PowerShell script. This value will be assigned to the myVariable parameter in the PowerShell script when it is executed.

What is the best practice for setting variables in PowerShell scripts from C#?

One common practice for setting variables in PowerShell scripts from C# is to use the AddScript method of the PowerShell class and pass in the script containing the variable assignments. Here is an example of how to set variables in a PowerShell script from C#:

using System; using System.Management.Automation;

class Program { static void Main() { using (PowerShell powerShell = PowerShell.Create()) { string script = "$myVariable = 'Hello, World'";

        powerShell.AddScript(script);
        powerShell.Invoke();
        
        var myVariable = powerShell.Runspace.SessionStateProxy.GetVariableValue("myVariable");
        
        Console.WriteLine(myVariable); // Output: Hello, World
    }
}

}

In this example, the AddScript method is used to set a variable $myVariable to the value 'Hello, World'. The Invoke method is then called to run the PowerShell script and set the variable. Finally, the GetVariableValue method is used to retrieve the value of the variable.

How to set variables with values passed from C# in PowerShell script?

You can set variables in a PowerShell script with values passed from C# by using the param() block at the beginning of the script. Here's an example of how you can do that:

  1. In your PowerShell script, define the parameters that you want to receive from C#:

param( [string]$param1, [int]$param2 )

  1. Use the variables $param1 and $param2 in your script as needed:

Write-Output "Parameter 1: $param1" Write-Output "Parameter 2: $param2"

  1. In your C# code, call the PowerShell script and pass the values for the parameters:

using (PowerShell powerShell = PowerShell.Create()) { powerShell.AddScript(@"C:\path\to\your\script.ps1"); powerShell.AddParameters(new Dictionary<string, object> { { "param1", "value1" }, { "param2", 123 } }); powerShell.Invoke(); }

By following these steps, you can set variables with values passed from C# in a PowerShell script.