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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.