r/PowerShell • • Nov 14 '25

How do I update PowerShell?

5 Upvotes

I need to install PowerShell version 7.5.4, but I don't know how. When I go to https://github.com/PowerShell/PowerShell/releases/tag/v7.5.4, I don't know which file to choose for Windows or how to install it. This hasn't happened to me before; it seems like something changed.


r/PowerShell • • Nov 14 '25

Script Sharing Parsing Json with duplicate keys with Windows Powershell

18 Upvotes

I ran into an API that returns Json with duplicate keys at work and haven't yet ported most necessary modules to Powershell Core which has -AsHashTable.

Went ahead and wrote this to deal with it. All duplicate keys at the same nested level get a number suffix so the json can be fed into ConvertFrom-Json.

Example output from running this script:

    # test 1
    id    : 50001
    test  : {@{id=50001; ID0=50001; Id1=50001; iD2=50001}}
    ID0   : 50001
    Id1   : 50001
    test2 : {@{id=50001; ID0=50001; Id1=50001; iD2=50001; test3=System.Object[]}}
    iD2   : 50001
    
    #test 2
    id  : blah
    iD0 : 1

The functions that do the work are Find-JsonDuplicates and Format-NormalizedJson. Called by putting Format-NormalizedJson between your Json string and ConvertFrom-Json like:

$jsonStringWithDuplicates | Format-NormalizedJson | ConvertFrom-Json

Script:

    # Comprehensive test of nested duplicates and duplicate
    # properties separated by other elements
    $testJson = @'
    {
        "id": 50001,
        "test": [
            {
                "id": 50001,
                "ID": 50001,
                "Id": 50001,
                "iD": 50001
            }
        ],
        "ID": 50001,
        "Id": 50001,
        "test2": [
            {
                "id": 50001,
                "ID": 50001,
                "Id": 50001,
                "iD": 50001,
                "test3": [
                    {
                        "id": 50001,
                        "ID": [
                            "50001"
                        ],
                        "Id": {
                            "blah": "50001"
                        },
                        "iD": [
                            50001
                        ]
                    }
                ]
            }
        ],
        "iD": 50001
    }
'@
    
    # Test of single occurrence of duplicate
    $testJson2=@'
    [
        {
            "id": "blah",
            "iD": 1
        }
    ]
'@
    
    function Find-JsonDuplicates {
        param(
            [string]$json
        )
        # levelCount is nested level
        $levelCount = -1
        $levelInstances = [System.Collections.ArrayList]::new()
        # levelInstance is for occurrences at same nested level
        $levelInstance = 0
        # build property keys
        $keyBuilder = [System.Text.StringBuilder]::new()
        $startQuote = $false
        $endQuote = $false
        $buildKey = $false
        $currentQuoteIndex = 0
    
        $jsonChars = $json.ToCharArray()
    
        $keyCollection = [System.Collections.ArrayList]::new()
        for ($i = 0; $i -lt $jsonChars.Count; $i++ ) {
            $currentChar = $jsonChars[$i]
    
            if ($buildKey -and !$currentChar.Equals([char]'"')) {
                $keyBuilder.Append($currentChar) | Out-Null
                continue
            }
    
            switch ($currentChar) {
                # Collect values between quotes
                '"' {
                    if (!$startQuote) {
                        $currentQuoteIndex = $i
                        $startQuote = $true
                        $buildKey = $true
                    }
                    elseif (!$endQuote) {
                        $endQuote = $true
                        $buildKey = $false
                    }
                }
                # Increment nested level and set or retrieve instance
                '{' {
                    $levelCount++
                    if ($levelInstances.Count - 1 -lt $levelCount) {
                        $levelInstance = 0
                        $levelInstances.Add(0) | Out-Null
                    }
                    else {
                        $levelInstances[$levelCount] = $levelInstances[$levelCount] + 1
                        $levelInstance = $levelInstances[$levelCount]
                    }
                }
                # Decrement nested level and retrieve the instance for the last nested level
                '}' {
                    $levelCount--
                    $levelInstance = $levelInstances[$levelCount]
                    $startQuote = $false
                    $endQuote = $false
                    # String was value and not key, reset builder
                    $keyBuilder.Clear() | Out-Null
                }
                ':' {
                    # Add property keeping track of its nested instance and startindex
                    if ($endQuote) {
                        $currentKey = $keyBuilder.ToString()
                        $keyCollection.Add(
                            [pscustomobject]@{
                                Level      = "$($levelCount)$($levelInstance)"
                                Key        = $currentKey
                                StartIndex = $currentQuoteIndex + 1
                            }
                        ) | Out-Null
                        $keyBuilder.Clear() | Out-Null
                        $startQuote = $false
                        $endQuote = $false
                    }
                }
                # String was value and not key, reset builder
                ',' {
                    $startQuote = $false
                    $endQuote = $false
                    $keyBuilder.Clear() | Out-Null
                }
            }
        }
    
        $duplicates = @($keyCollection | Group-Object Level, Key | Where-Object { $_.Count -gt 1 })
    
        $outCollection = [System.Collections.ArrayList]::New()
    
        foreach ($d in $duplicates) {
            $outCollection.AddRange(@($d.Group[1..($d.Count)])) | Out-Null
        }
        $outCollection = $outCollection | Sort-Object StartIndex
        return , $outCollection
    }
    
    Function Format-NormalizedJson {
        [CmdletBinding()]
        param(
            [parameter(ValueFromPipeline)]
            [string]$json
        )
    
        process {
            $duplicates = Find-JsonDuplicates $json
            # Adding characters to the Json offsets the subsequent index
            # keep track of offset
            $suffixOffset = 0
            $levelKeyCounter = @{}
       
            foreach ($d in $duplicates) {
                # Maintain increment consistency with Key and Level
                if ($levelKeyCounter.ContainsKey("$($d.Key):$($d.Level)")) {
                    $currentCounter = $levelKeyCounter["$($d.Key):$($d.Level)"]
                }
                else {
                    $currentCounter = 0
                }
                # Replace the duplicate property with numbered suffix
                $json = $json.Substring(0, $d.StartIndex + $suffixOffset) `
                    + "$($d.Key)$currentCounter" `
                    + $json.Substring($d.StartIndex + $d.Key.Length + $suffixOffset, $Json.Length - ($d.StartIndex + $d.Key.Length + $suffixOffset))
    
                $suffixOffset += $currentCounter.ToString().Length
                $currentCounter++
                $levelKeyCounter["$($d.Key):$($d.Level)"] = $currentCounter
            }
    
            return $json
        }
    }
    
    $testJsonUpdated = $testJson | Format-NormalizedJson | ConvertFrom-Json
    
    $testJsonUpdated
    
    $testJsonUpdated2 = $testJson2 | Format-NormalizedJson | ConvertFrom-Json
    
    $testJsonUpdated2

r/PowerShell • • Nov 14 '25

Private items in share Mailbox prevent access for Otter users - looking for a PowerShell command

0 Upvotes

Hello everyone,

I’m an IT administrator in our company and currently facing the following issue: We are using a shared mailbox that several employees have FullAccess permissions to. However, one user is accidentally marking incoming or forwarded emails as “Private.” As a result, other team members are unable to see these messages, even though they have the correct permissions on the mailbox.

Since Outlook hides private items by default in shared mailboxes, these emails remain invisible to the rest of the team. This causes workflow disruptions because important information is not accessible to everyone who needs it.

From what I understand, the issue can be resolved by granting the affected users the “CanViewPrivateItems” permission on the relevant folder (e.g., Inbox) of the shared mailbox.

Therefore, I am looking for a PowerShell command that allows me to assign this permission so that team members are able to view private items within the shared mailbox.

Has anyone dealt with this before, or does someone have a working command for Exchange Online (or on-prem) that reliably enables this permission?

Thanks in advance!

Best regards, Daniel


r/PowerShell • • Nov 14 '25

Question Windows 11 install with autounattend.xml - Win Updates the value is out of range

1 Upvotes

If I want to install Windows 11 with autounattend.xml, I run Windows updates with a Powershell script.

However, I get this error: the value is out of range

How can I fix the problem?


r/PowerShell • • Nov 13 '25

Question Encrypting and decrypting a string with Powershell using a text password

18 Upvotes

Hi all,

what is the best way to perform password based encryption and decryption with Powershell?

Here's some context:

I have a powershell utility script that performs some API call to a server. These calls include a token, which at the moment is for convenience stored in plaintext inside the script. Since I need to share this script with other possibly untrusted users, I would like to store this token encrypted, so that the user launching the script needs to insert the right key (password) to decrypt the token and successfully execute the API calls.

In short, I would like to:

  • Take a plaintext string and encrypt it using a text password
  • Make the inverse operation
  • All of this using only Powershell v 5.1

I think it shouldn't be hard to do it, but I couldn't find a way on my own looking on the web, can anyone help me with this? Does it even make sense or is there a better way to obfuscate the token and request authorization for launching the script?

Much appreciate anyone taking the time!


r/PowerShell • • Nov 14 '25

Change keyboard layout while not admin

1 Upvotes

Hi team,

Doing an internship overseas and want to be able to run a script on the random computers that I will be using to convert the keyboard layout to one I'm familiar with.

I've seen plenty of methods on changing the language in powershell, but I want to use the US international keyboard so that I can still type in other languages, just with a familiar layout. How can I do this? Will not being admin on the computers I am using be an issue?

Cheers.


r/PowerShell • • Nov 14 '25

“Member of” export

0 Upvotes

Hi, I have been asked to provide a list of groups each user in a CSV is a member of, is there any way of doing this in bulk? Or would I have to do it individually? Thanks in advanced


r/PowerShell • • Nov 13 '25

"powershell has stopped working" pops up twice on every startup

1 Upvotes

title aside, I've tried what other posts have said (installing Malware Bytes and using its scan feature) but no threats have been detected. is there anything i can do to fix this? it only started a few days ago and i haven't downloaded anything sketchy

forgot to mention that I get the same thing when starting power shell manually including as an administrator