Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To pass a JSON text as a parameter to PowerShell, you can follow these steps:

  1. Create a new file with a .json extension and save the JSON text in it.

  2. In your PowerShell script, use the Get-Content cmdlet to read the JSON text file and store the contents in a variable:

$json = Get-Content -Path "C:\path\to\my\json\file.json" -Raw

Note: The -Raw parameter is used to read the entire contents of the file as a single string.

  1. Use the ConvertFrom-Json cmdlet to convert the JSON text into a PowerShell object:
$obj = $json | ConvertFrom-Json
  1. Once you have the PowerShell object, you can use its properties and methods to extract and manipulate the data as needed.

Alternatively, you can pass the JSON text as a string parameter directly to your script using single quotes:

.\myScript.ps1 -json '{ "name": "John", "age": 30 }'

In your script, you can retrieve the parameter value using the $args variable:

$json = $args[0]
$obj = $json | ConvertFrom-Json