1

Running this code

get-service | Select-Object -Property displayname, status, name | convertTo-Json

results for instance in an output like this:

{
    "DisplayName":  "Adobe Acrobat Update Service",
    "Status":  4,
    "Name":  "AdobeARMservice"
},
{
    "DisplayName":  "Adobe Flash Player Update Service",
    "Status":  1,
    "Name":  "AdobeFlashPlayerUpdateSvc"
},

Is it possible to return the keys in lowercase?

2 Answers 2

6

You could use calculated properties:

get-service | Select-Object -Property @{n='displayname';e={$_.displayname.tolower()}}, status, name | convertTo-Json

what this does is changes the way output is formatted

ps. you could repeat that with all properties pps. https://blogs.technet.microsoft.com/josebda/2014/04/19/powershell-tips-for-building-objects-with-custom-properties-and-special-formatting/

2
  • Thank you. Is it possible to keep the values like they are(only format the keys to lowercase)? With your approach both, keys and values, are formatted to lowercase.
    – Matt
    Apr 29, 2018 at 15:59
  • 1
    just remove .tolower() from the expression. @{n='displayname';e={$_.displayname}}
    – 4c74356b41
    Apr 29, 2018 at 17:45
2

You can use Regular Expressions to solve the requirement:

#Create a variable with the info
$json = Get-Service | Select-Object -Property displayname, status, name | convertTo-Json
#Use RegEx
[regex]::Replace(
    $json,
    '(?<=")(\w+)(?=":)',
    {$args[0].Groups[1].Value.ToLower()}
)

And the output is like this:

[
    {
        "displayname":  "Application Identity",
        "status":  1,
        "name":  "AppIDSvc"
    },
    {
        "displayname":  "Application Information",
        "status":  4,
        "name":  "Appinfo"
    },
2
  • seems like a bash way of doing things, aka suboptimal
    – 4c74356b41
    Jan 17, 2020 at 9:43
  • This may be very bash like but handles all json keys even ones not planned for.
    – Abu Belal
    Oct 12, 2021 at 16:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.