ahill
ahill

Reputation: 1846

cmd.exe powershell HashTable

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?

Upvotes: 8

Views: 3941

Answers (2)

Keith Hill
Keith Hill

Reputation: 201832

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

Upvotes: 14

mjolinor
mjolinor

Reputation: 68301

The only thing that comes to mind is to save the hash table as xml, and pass it the file name.

Upvotes: 0

Related Questions