Testing a bulk rename ps1 script. Using the -WhatIf to make sure code doesn’t do a wild run-away rename. However, when I run the script through a debugger (saw that running straight through displayed nothing); the command with the -WhatIf appears to execute but the “whatif: ” console message never appears. When I run it through, step by step, the debugger, I break on the command in question, step into it, ISE flashes and the cursor moves to the next statement – the -WhatIf message doesn’t appear in the output, and the $? (I think that shows the last command execution status?) is True.
Where could the WhatIf output have gone, and/or what hvae I messed up in the script to make it malfunction?
Also, is there a way to display the $_.Fullname value as a kind of log entry when -WhatIf is removed?
NOTE:
- Using PowerShell 7.4.2
- All variables in the command in question are correctly assigned – checked values prior to stepping into command. I removed extraneous code to simplify this post.
[CmdletBinding()]
# Rename files in directory tree
# Rename all files in specified directory and sub directories using the old and new patterns
# *** Add "-Recurse " once proven rename works as desired (to prevent wild rename run-away)
#******
Get-ChildItem $workPath -File -Recurse `
| ForEach-Object {Rename-Item $_.Fullname `
-NewName ($_ -replace $w, $n) -WhatIf
}
# if (not $?) {
# write-host "*** Rename to $newName did NOT work"
# }
}
"Done"
######################
Terminal output
PS X:PS Rename> # Terminal cursor position prior initiating debug
PS X:PS Rename> # Terminal cursor position AFTER initiating debug
# No terminal output during execution
NewbieBAT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.