Powershell script to add .old to folders?

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.
xouvangAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LearnctxEngineerCommented:
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 (
	[Parameter(Mandatory=$true,Position=1)]
	[string]$ComputerName = $null,
	[Parameter(Mandatory=$true,Position=0)]
	[string]$Identity = $null
)

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

if (Test-Connection -Quiet -Count 1 -ComputerName $ComputerName)
{
	if (Test-Path -Path $Path)
	{
		try
		{
			Rename-Item -Path $Path -NewName "$($Path).old" -ErrorAction Stop
			Write-Host "$($Path) renamed to $($Path).old"
		}
		catch
		{
			Write-Host "Error: $($_.exception.message)"
		}
	}
	else
	{
		Write-Host "Path $($Path) does not exist."
	}
}
else
{
	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
0
xouvangAuthor Commented:
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


Thanks!
Capture.JPG
0
LearnctxEngineerCommented:
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.
0
xouvangAuthor Commented:
ahhh ok got it

thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.