Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmd.exe powershell HashTable

Tags:

powershell

cmd

Is there a way in PowerShell to pass a HashTable as an argument when being invoked with cmd.exe?

I want to invoke a script like this:

powershell "& 'C:\path\to\file.ps1 arg1 arg2 arg3 arg4'"

Where arg4 is a HashTable. Is this possible?

like image 237
andyhky Avatar asked Feb 18 '11 02:02

andyhky


People also ask

What is PowerShell Hashtable?

A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key/value pairs. For example, a hash table might contain a series of IP addresses and computer names, where the IP addresses are the keys and the computer names are the values, or vice versa.

Can you run cmd in PowerShell?

This includes script files that may require other shells to work properly. For example, if you run a Windows batch script ( . cmd file) in PowerShell, PowerShell runs cmd.exe and passes in the batch file for execution.

How do I make a hash table in PowerShell?

To create a hash table in PowerShell, you'll use an @ symbol followed by an opening curly brace and a closing curly brace as shown below. Here you can see my hash table is now three lines with a key/value pair in the middle. It can also be represented on one line as well.


1 Answers

Given a script (foo.ps1) like this:

param($a1, $a2, $a3, [hashtable]$a4)

"a1 is $a1"
"a2 is $a2"
"a3 is $a3"
"a4 is "
$a4

You can invoke it from cmd.exe like so specifying a hashtable as the fourth parameter:

C:\> powershell -command "& {c:\foo.ps1 1 2 three @{name='John';age=45}}"
a1 is 1
a2 is 2
a3 is three
a4 is

Name                           Value
----                           -----
name                           John
age                            45
like image 60
Keith Hill Avatar answered Oct 02 '22 01:10

Keith Hill