Author Posts

September 4, 2018 at 7:07 pm

Hello, I'm trying to rename files that match values in column one of a csv adding the value in column 3 to the beginning of the file name leaving the rest of the file name intact. Here is what I have so far. I cant seem to figure out the Rename-Item. I get the error "Cannot evaluate parameter 'NewName' because its argument is specified as a script block and there is no input."

# Common Paths

$PathRoot = "C:\Temp\HIPAASuite" #Where the files are to rename

 

# Get csv file

$ClientAccounts = Import-CSV -path "\\server\some\path\to\csv\file.csv"

 

# Get each file and rename it

ForEach($row in $ClientAccounts)

{

$CurrentClientTaxId = $row[-1].TaxId

$CurrentClientName = $row[-1].ClientName

#loop through files

$FileExists = Test-Path -Path "$PathTotal\*$CurrentClientLB_Number*" #See if there is a file.

If ($FileExists -eq $true) #The file does exist.

{

#ReName File

Rename-Item -Path $PathRoot -NewName {$CurrentClientName + " " + $_.name}

}

 

}

September 4, 2018 at 7:24 pm

You don't need to use a script block there for the new name parameter – just need to create the string that you want to work with. As per your original code above with a space between currentClientName and $_.name:

Rename-Item -Path $PathRoot -NewName "$($CurrentClientName) $($_.name)"