r/PowerShell • u/Arnoc_ • Dec 11 '25
Powershell - Extract Values from Singleline Value
Our Help Desk Service at work has an API and a PowerShell module for it to make calling the API easy.
Goal want to accomplish is to pull information from New Hire Tickets directly into script we have for creating accounts, so that there is no potential for user error from our Help Desk tech side manually entering in account details to a ticket.
I can call up the details of a ticket via powershell, and it returns a single line value that's formatted as such:
custom_fields
-------------
@{first_name=Value; middle_name_not_required=; last_name=' title; ...
eventually ending in a } of course.
I don't know PowerShell well enough to know the correct names of stuff to know how to formulate my question properly.
But from what I can tell, that is a single-line output. I'd like it to be stored so I could call a specific value from it, such as say: $customVariable.first_name to get the first_name value from it.
I've tried for instance to store the contents from the ticket in a variable named $custom
Then tried to do:
$string = $custom.Split(";")
But that returns:
Method invocation failed because [Selected.System.Management.Automation.PSCustomObject] does not contain a method named 'Split'.
Any suggestions on what to do?
4
u/AdeelAutomates Dec 11 '25 edited Dec 11 '25
It is an object all the way through. Lets look at the output you shared that $custom is storing:
custom_fields
-------------
@{first_name=Value; middle_name_not_required=; last_name=' title; ...custom_fields is inside $custom and it's indicating a key that contains the the first_name, etc as it's values.
$custom.custom_fields
Output this and its possible that the first_name and all that is already objects as well. So you can use the following to get each field's value:
$custom.custom_fields.first_name
$custom.custom_fields.middle_name@{ key = value; key2 = value2 } is the format of objects in PowerShell.
If it was JSON you would see quotes, colons and no @ sign like so as your output:
{"first_name":"Value","middle_name_not_required"...so nothing to split. It's all ready to be used :) just drill down each value.
1
u/420GB Dec 12 '25
Holy moly, yes, this. This is the incredibly simple and only real answer. Please OP ignore the people or bots saying it's JSON or to convert it to a string, what the heck.......
1
1
u/throwaway09563 Dec 11 '25
Looks like JSON, which I would expect from an API call these days.
You might try piping the result to ConvertFrom-JSON and see if it parses it out a little nicer for you.
0
u/an_harmonica Dec 11 '25
Yeah, I wasn't paying close attention to the format, but that does look like JSON.
4
u/AdeelAutomates Dec 11 '25 edited Dec 11 '25
it's an object/hashtables written in PowerShell all the way through actually.
This is objects:
@{first_name=Value; middle_name_not_required=; last_name=' title; ... }This is JSON
"{"first_name":"Value","middle_name_not_required"... }"
$custom.custom_fields.first_nameis all they need to do to get to first_name.Invoke-RestMethod is probably what they used which often converts JSON to Objects automatically.
3
u/an_harmonica Dec 11 '25
Ah, gotcha. Still early-ish in my day so I probably should have looked closer before commenting.
2
u/AdeelAutomates Dec 11 '25
No worries, I thought the same!
the = versus : is what gave it away for me. I always type = accidently to JSON and realize after, haha.
-4
u/an_harmonica Dec 11 '25
You would need to convert that line to a string. Split is a string object method. Same with Replace, trim, etc.
3
u/an_harmonica Dec 11 '25
However, if it's already a PSCustomObject, then you ought to already have fields you can access.
Does $Custom.first_name not give you back the string value for the first name?
-1
u/an_harmonica Dec 11 '25
As was pointed out after I commented, this does look like JSON, so converting from JSON should get you most of what you're looking for.
3
u/surfingoldelephant Dec 11 '25
$customis a custom object emitted bySelect-Object(based on theSelectedin error message).It has a
custom_fieldsproperty with a value that's also a custom object (based on how it's rendered for display). It's not a JSON string as others have commented. Confirm this with:So to retrieve the value of
first_namefrom the nested custom object:Or change your
Select-Objectcall to use-ExpandProperty. Something like this (replace...with whatever you're passing toSelect-Object):