0

Hello i have powershell command that looks like this.

$result = Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information" | Out-String

And output like this

Id                  : xxx69add-xxd1-xx43-xxba-xxcd2fac8790
Title               : Create customer tracking list
Description         : Creates list for tracking customer contact information
Content             : xxx
Version             : 0
IsSiteScriptPackage : False

What I want to do is to take Id somehow from it but I don't know-how. I don't know how to output this as an array so i can use $result[0] or any other way how to extract it.

Thanks for any help.

  • 2
    Replace `Out-String` with `Select -Expand Id` – Mathias R. Jessen Jul 13 '20 at 10:56
  • 1
    Does this answer your question? [Select the values of one property on all objects of an array in PowerShell](https://stackoverflow.com/a/48888108/1701026). From PSv3+ you can also use [member enumeration](https://stackoverflow.com/a/44620191/45375): `$result = (Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information").Id` – iRon Jul 13 '20 at 13:24

1 Answers1

1

Out-String turns all the input into a string, so not very useful here.

Either enclose the expression in parentheses and reference the Id parameter with the . member access operator:

$result = (Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information").Id

Or grab the Id property value with Select-Object:

$result = Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information" |Select-Object -ExpandProperty Id

... or by using ForEach-Object member invocation:

$result = Add-SPOSiteScript -Title "Create customer tracking list" -Content $site_script -Description "Creates list for tracking customer contact information" |ForEach-Object Id
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206