Home > Software design >  powershell string replace inline or new loop
powershell string replace inline or new loop

Time:10-13

Whats the best way to do some string replacements in the 'fullname'?

something like .Replace($oldString, $newString) but it needs to be done after the MD5 is already calculated.

Or is it just better to do another ForEach-Object after my MD5 process is done?

| select-object name, fullname, @{Name = "MD5"; Expression = { (Get-FileHash $_.FullName -Algorithm MD5).Hash.ToLower() } }

CodePudding user response:

it needs to be done after the MD5 is already calculated

I don't think you can depend on another calculated property when creating a calculated property, at least not within the same pipeline step. Each calculated property will only be available when the current pipeline step has finished as a whole.

What you could do is chain two Select-Object statements like this:

Get-ChildItem -file |
select-object fullname, name, @{Name = "MD5"; Expression = { (Get-FileHash $_.FullName -Algorithm MD5).Hash.ToLower() } } | 
select-object name, MD5, @{Name = 'fullname'; Expression = { <# do something with $_.fullname and $_.MD5 #> }}
  • Related