We help IT Professionals succeed at work.

Powershell script to add .old to folders?

xouvang asked
Hi all,

I need a script that adds .old to a folder. I already got the script to remove any folder with .old in them.
I have users that get corrupted profiles. Instead of trying to remote into their computer and rename their profiles to .old, is there a script to add .old their profile folder?

What I want to do is launch the script change the name and the computer. Once I make that change I run the script and it will automatically add .old to their profile.

So far I have get-childitem \\computer\c$\users - filter "profile folder name" | {where{$_.PsIsContainer} | rename-item

That's as far as I got, please let me know if I'm using the wrong script.
Watch Question

Messaging and Directory Services
Run powershell as administrator and then try :
$profileFolderName = "profile folder name"
$folder = (get-childitem "\\computer\c$\users" -filter $profileFolderName).FullName
rename-item -path $folder -newName $($folder+".old") -Force

Open in new window

You're using -filter incorrectly; -filter is parameter so there can be no space between the dash and filter. You would use:

gci -filter profile*

That said you know what you are trying to target so why not target it directly and add a few checks while you're at it: Is the computer online? Does the path exist? Does the rename work. Error handling to a degree.

param (
	[string]$ComputerName = $null,
	[string]$Identity = $null

$Path = "\\$($ComputerName)\c$\Users\$($Identity)"

if (Test-Connection -Quiet -Count 1 -ComputerName $ComputerName)
	if (Test-Path -Path $Path)
			Rename-Item -Path $Path -NewName "$($Path).old" -ErrorAction Stop
			Write-Host "$($Path) renamed to $($Path).old"
			Write-Host "Error: $($_.exception.message)"
		Write-Host "Path $($Path) does not exist."
	Write-Host "Device $($ComputerName) is offline or not reachable."

Open in new window

Save as a .ps1. Execute like any other script.

.\script.ps1 -Identity Username -ComputerName computer1


Let's say computer is ANC54543 and the folder is TestUser

@LearnCTX - Ok I tried to run the codes and are getting errors. Attached is the screenshot below

@Raheman Mohammed Abdul - Thanks! Your code works

You are pasting the script into the powershell console. You need to save it as a .ps1. Fur example script.ps1.

You execute it from the console as follows:

C:\temp\script.ps1 -identity TestUser -conputername ANC54543

Replace c:\temp with the path to the script.


ahhh ok got it