user3642200
user3642200

Reputation: 21

Error when renaming folder in user directory using powershell

I am very new to PowerShell but I wrote a script that renames 4 folders in each users network home directory. The users SAM accounts are pulled from a text file.

The script works but I get errors like this after the script runs. Why am I getting this?

"Get-Item : Cannot find path 'C:\Portability' because it does not exist. At line:3 char:5 + Get-Item -Path "$line\Portability" | Rename-Item -NewName "Portability$(Get- ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Portability:String) [Get-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"

Here is the script:

#Import AD PowerShell Module

Import-Module ActiveDirectory

#Get Home Path for each user

$GetHD = Get-Content C:\Temp\UserList.txt | ForEach {Get-ADUser $_ -properties HomeDirectory | Select HomeDirectory} > C:\temp\UserInfo.txt

#Copy list of paths to C:\temp\dir.txt and safe content to variable $hdirpath

Get-Content C:\temp\UserInfo.txt | Select-Object -Skip 3 | Foreach {$_.TrimEnd()} | Out-File C:\Temp\dir.txt

$hdirpath = Get-Content C:\Temp\dir.txt

#Rename folder if exist adding current date at end of folder

ForEach ($line in $hdirpath) {

Get-Item -Path "$line\Portability" | Rename-Item -NewName "Portability$(Get-Date -Format ""MMddyyyy"")" -WhatIf
Get-Item -Path "$line\Profile" | Rename-Item -NewName "Profile$(Get-Date -Format ""MMddyyyy"")" -WhatIf
Get-Item -Path "$line\Profile.v2" | Rename-Item -NewName "Profile.v2$(Get-Date -Format ""MMddyyyy"")" -WhatIf
Get-Item -Path "$line\TSProfile.v2" | Rename-Item -NewName "TSProfile.v2$(Get-Date -Format ""MMddyyyy"")" -WhatIf

}

Upvotes: 1

Views: 891

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13227

You say "Rename folder if exist" but you don't check that the folders exist before you run commands against them, which is why you are getting the error:

Cannot find path 'C:\Portability' because it does not exist"

You can update your code with a simple if along with Test-Path to check the folder exists and only try to rename if it does:

ForEach ($line in $hdirpath) {
    if(Test-Path "$line\Portability"){ Rename-Item -Path "$line\Portability" -NewName "Portability$(Get-Date -Format ""MMddyyyy"")" -WhatIf }
    if(Test-Path "$line\Profile"){ Rename-Item -Path "$line\Profile" -NewName "Profile$(Get-Date -Format ""MMddyyyy"")" -WhatIf }
    if(Test-Path "$line\Profile.v2"){ Rename-Item -Path "$line\Profile.v2" -NewName "Profile.v2$(Get-Date -Format ""MMddyyyy"")" -WhatIf }
    if(Test-Path "$line\TSProfile.v2"){ Rename-Item -Path "$line\TSProfile.v2" -NewName "Installs$(Get-Date -Format ""MMddyyyy"")" -WhatIf }
}

EDIT:

Here's a version that removes the need for multiple txt files as interim steps, and adds a function for the rename to avoid lines of duplicate code.

I've removed two bits, as I wasn't sure why you would want to skip the first three Users in your list (Select-Object -Skip 3), or why you need to remove any trailing spaces (Foreach {$_.TrimEnd()}) as this will mean that the HomeDirectory path will not match when you try the rename.

Function Rename-Folder
{
    Param (
        [Parameter(Mandatory=$true)]
        [string]$Path,
        [Parameter(Mandatory=$true)]
        [string]$NewName
    )

    if(Test-Path $Path){ Rename-Item -Path $Path -NewName $NewName}
}

Import-Module ActiveDirectory

#Get Home Path for each user
$HomeDirectories = Get-Content "C:\Temp\UserList.txt" | ForEach {Get-ADUser $_ -properties HomeDirectory | Select HomeDirectory}

#Rename folder if exist adding current date at end of folder
ForEach ($line in $HomeDirectories) {
    Rename-Folder -Path "$line\Portability" -NewName "Portability$(Get-Date -Format ""MMddyyyy"")"
    Rename-Folder -Path "$line\Profile" -NewName "Profile$(Get-Date -Format ""MMddyyyy"")"
    Rename-Folder -Path "$line\Profile.v2" -NewName "Profile.v2$(Get-Date -Format ""MMddyyyy"")"
    Rename-Folder -Path "$line\TSProfile.v2" -NewName "Installs$(Get-Date -Format ""MMddyyyy"")"
}

Note: I don't use Home Directories in my AD setup so can't test this fully, but it works fine when testing in isolation.

Upvotes: 5

Related Questions