Assuming that each line's list of key-value pairs only contain values without embedded whitespace or quoting:
# Sample input line.
$line = 'ts=2019-01-16 network=1.1.1.1 system=irgendwas pid1=100 bugReq=dasf something=else maybe=this'
# Parse the line into key-value pairs and create a variable for each.
$i = 0
foreach ($keyOrValue in $line -split '[= ]') {
if ($i++ % 2 -eq 0) { $varName = $keyOrValue }
else { Set-Variable $varName $keyOrValue }
}
# $ts now contains '2019-01-16', $network '1.1.1.1', $system 'irgendwas', ...
Note how I've modified your sample input line slightly to change pid to pid1, because PowerShell won't allow you to create a $PID variable, because it is an automatic variable reflecting the current session's PID (process ID).
Another option (which would also avoid the variable-name conflict) is to create a hashtable for each input line:
# Sample input line.
$line = 'ts=2019-01-16 network=1.1.1.1 system=irgendwas pid=100 bugReq=dasf something=else maybe=this'
# Parse the line into key-value pairs and create a variable for each.
$htValues = @{} # Initialize the hashtable.
$i = 0
foreach ($keyOrValue in $line -split '[= ]') {
if ($i++ % 2 -eq 0) { $varName = $keyOrValue }
else { $htValues[$varName] = $keyOrValue }
}
# $htValues now has keys 'ts', 'network', 'system' with corresponding
# values, so you can access $htValues.ts to get '2019-01-16', for instance.
This approach has the added advantage of lending itself to collecting the hashtables created for the individual lines in an overall array (e.g., $hashTableArray = foreach ($line in ...) { ... } - though with a really big file that may not be an option.
Borrowing an idea from Lee_Dailey's answer, you can alternatively use the ConvertFrom-StringData cmdlet to create the hashtable, after first placing each key-value pair onto its own line with the help of the -replace operator:
$htValues = ConvertFrom-StringData ($line -replace ' ', "`n")
The caveat re ConvertFrom-StringData is that it interprets \ chars. as starting escape sequences; for instance, a value such as b\c breaks the command:
Convertfrom-StringData 'a=b\c' # ERROR: "parsing 'b\c' - Missing control character."
On the plus side, use of ConvertFrom-StringData is much faster than manual parsing with foreach.
As an aside: PowerShell's Get-Content cmdlet for reading lines one by one (by default) is convenient, but slow.
To process a (large) text file's lines one by one more quickly, use :
$file = 'file.txt'
foreach ($line in [System.IO.File]::ReadLines((Convert-Path $file))) {
# ...
}
ConvertFrom-StringDatacmdlet. if you replace the space delimiter with newlines, the cmdlet will give you a hashtable that can be used to generate a PSCustomObject. the result will be a nice, clean object that can be used as any other. the only gotcha is the properties will be in no particular order.