How to add dashes to a string

I’m generating random numbers using get-random. I want to add dashes every 4 numbers. I need some help in how to do this. Thanks.

have you written any code to do that yet? Please post

#region input
$myInput        = Get-Random
$SplitEvery     = 4
$SplitCharacter = '-'

#region process
$myString = [String]$myInput
$i = 1
$myOutput = $myString.ToCharArray() | % { if ($i % $SplitEvery -eq 0) { "$_$SplitCharacter" } else { $_ }; $i++ }

#region Output
$myOutput = $myOutput -join ''
#remove trailing SplitCharacter
if ($myOutput[-1] -eq $SplitCharacter) { $myOutput = $myOutput[0..($myOutput.Length-2)] -join ''}

If you don’t fully comprehend every line above, please ask…

So far my code was “Get-Random -count 24”. I played around with split and nothing I did worked. I remembered this website after erased everything. Thanks!!!

How’s this? Replace any 4 characters not at the end of the line.

'12345678911234567890' -replace '(....(?!$))','$1-'


That did work. What is going on here ‘(…(?!$))’,’$1- ? Where can I get more info? Thanks!!!

… is any 4 characters, () let’s me refer to it later as $1, (?!$) means lookahead and match not, ! the end of the line, $. Maybe plug it into