Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To transmit an object as a parameter for a function in PowerShell used for GUI, you can follow the steps below:

  1. Define a parameter for the function that accepts an object:
function MyFunction {
    param(
        [Parameter(Position=0,Mandatory=$true)]
        [Object]$myObject
    )
    # Function code here
}
  1. In your GUI code, create the object that you want to pass as a parameter:
$myObject = New-Object -TypeName PSObject -Property @{
    Name = 'John'
    Age = 30
    Location = 'New York'
}
  1. Call the function and pass in the object as the parameter:
MyFunction -myObject $myObject

The function can then access the properties of the object using dot notation, for example:

function MyFunction {
    param(
        [Parameter(Position=0,Mandatory=$true)]
        [Object]$myObject
    )
    Write-Host "Name: $($myObject.Name)"
    Write-Host "Age: $($myObject.Age)"
    Write-Host "Location: $($myObject.Location)"
}