It is annoying that -split has to be in the front with no arguments, and in the back with a custom delimiter. Here’s a cut function I made, like the unix command. So you can just say “string | cut -f 3”:
function cut {
param(
[Parameter(ValueFromPipeline=$True)] [string]$inputobject,
[string]$delimiter='\s+',
[string[]]$field
)
process {
if ($field -eq $null) { $inputobject -split $delimiter } else {
($inputobject -split $delimiter)[$field] }
}
}
Hmmm … call me picky but both of these approaches would even match “illegal” Mac addresses like “00:0c:ff:ed:8e:78:8e:78” or “00:0c:ff:ed:8y:78:xz:78” or “8y:78:xz:78”, right?
You are right, it would. In context I saw no indication that it was user generated input, nor did I see request for content validation. I only saw request to pull the MAC from the string, which the regex accomplishes. If MAC format validation is required, then yes, it would be the more appropriate option.