How to convert to UInt64 from a string in Powershell? String-to-number conversion

To complement Sean’s helpful answer: It is only the type constraint of your result variable ([uint64] $ConvertedMemory = …) that ensures that ($MemoryFromString / 1) is converted to [uint64] ([System.UInt64]). The result of expression $MemoryFromString / 1 is actually of type [int] ([System.Int32]): PS> (‘1gb’ / 1).GetType().FullName System.Int32 Therefore, to ensure that the expression by … Read more

Making a PowerShell POST request if a body param starts with ‘@’

You should be able to do the following: $params = @{“@type”=”login”; “username”=”[email protected]”; “password”=”yyy”; } Invoke-WebRequest -Uri http://foobar.com/endpoint -Method POST -Body $params This will send the post as the body. However – if you want to post this as a Json you might want to be explicit. To post this as a JSON you can specify … Read more

Set-Content appends a newline (line break, CRLF) at the end of my file

tl;dr (PSv5+; see bottom for older versions): (Get-Content webtemp.config) -replace ‘database=myDb;’, ‘database=newDb;’ -join “`n” | Set-Content -NoNewline -Force web1.config Note: Replace “`n” with “`r`n” if you want Windows-style CRLF line endings rather than Unix-style LF-only line endings (PowerShell and many utilities can handle both). In PSv5+, Set-Content supports the -NoNewline switch, which instructs Set-Content not … Read more

Iterating through a JSON file PowerShell

PowerShell 3.0+ In PowerShell 3.0 and higher (see: Determine installed PowerShell version) you can use the ConvertFrom-Json cmdlet to convert a JSON string into a PowerShell data structure. That’s convenient and unfortunate at the same time – convenient, because it’s very easy to consume JSON, unfortunate because ConvertFrom-Json gives you PSCustomObjects, and they are hard … Read more