[POWERSHELL] check rename directory

Hello ,

I want to know one thing. I realize a script and I want to check if the directory is corretly rename.

For exemple I creat this one : $test | rename-item -NewName{$_.name -replace “exemple”,“training”}

However as soon as the directory is modified the first time, if I start the schrip there is a error message which write "the item is already rename " something like that. To avoid the pollution of my display, I want to do include a conditionnal. But, I don’t know what I have to put in this “If” I want to do something like that :

If( $_.name -eq "training ") {

Write-Host "The directory is already rename"

}

 

But, I know, this one can’t work. Do you have an idea ? I think I missed something easily understandable btu i don’t know what.

Thanks to you !

aa, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

You may post your actual code or at least the relevant part of it and the complete error message you get.

Explain and show all of the code. What is $test? You should not need to see if something is renamed as you should be only finding folders named ‘exemple’ to do the rename.

The code is just about a directory which is in a place and I want to rename this directory. As soon as it’s rename I want to do a conditionnal "If "to verify if the directoy is correctly rename.

The variable $test is a random name for my example $test can be equal to “C:\User\Program” It’s just the place where we can find the directory that we want to rename

By default the cmdlet Rename-Item does not generate any output. But you can change this by using the parameter -PassThru. With this you can check the generated object if it’s the expected result. Please read the complete help including the wexamples to learn how to use it. You should read the help for about_if as well to get your task done. :wink:

The error should be captured to determine success\fail, doing a Test-Path isn’t necessary:

$path ='C:\Scripts\Foo'

try {
    $results = Rename-Item -Path $path -NewName Blah -Force -ErrorAction Stop -PassThru
    'Successfully renamed folder {0} to {1}' -f $path, $results.FullName
}
catch {
    'Problem renaming folder {0}. {1}' -f $path, $_
}

Output from success and then a re-run but the folder doesn’t exist, because it was renamed…

PS C:\Users\rasim> 
$path ='C:\Scripts\Foo'

try {
    $results = Rename-Item -Path $path -NewName Blah -Force -ErrorAction Stop -PassThru
    'Successfully renamed folder {0} to {1}' -f $path, $results.FullName
}
catch {
    'Problem renaming folder {0}. {1}' -f $path, $_
}
Successfully renamed folder C:\Scripts\Foo to C:\Scripts\Blah\

PS C:\Users\rasim> 
$path ='C:\Scripts\Foo'

try {
    $results = Rename-Item -Path $path -NewName Blah -Force -ErrorAction Stop -PassThru
    'Successfully renamed folder {0} to {1}' -f $path, $results.FullName
}
catch {
    'Problem renaming folder {0}. {1}' -f $path, $_
}
Problem renaming folder C:\Scripts\Foo. Cannot rename because item at 'C:\Scripts\Foo' does not exist.

Thanks for your help I’ll test this method tomorrow !