Delete Profiles on servers using powershell

Hi Guys,

We have 3 Remote Desktop Servers, RDS01, RDS02, RDS03, and users have a roaming profile that is on our Fileandprintserver01, when we have to recreate a profile we have delete it off the RDS01,02 and 03 servers as well as the Fileandprintserver01.

instead of this tedious process is there a way we can automate this via powershell?

Thank you in advance.
chglAsked:
Who is Participating?
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.

SubsunCommented:
0
chglAuthor Commented:
thanks, i have founds this:

(Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\test-*'}).Delete()


however will this also remove the SID from the registry?
0
Pradeep DubeyConsultantCommented:
Below tool will help you to delete profiles and provide logs for further audit.
its very good and also working (tested on win7-2008 servers)

http://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool/
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

SubsunCommented:
Yes it will delete the profile and registry entries.. But I am not sure about the exact registry entries.. You can try by removing a test account and see if it serve your purpose..
0
chglAuthor Commented:
Hi I cant get the code below to work, please help

$username=Read-host "Please Enter Username: "

$PServers="CIEH-RDS01","CIEH-RDS02","CIEH-RDS03","cieh-fnp01"

$Pservers | foreach {(Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\$username*}).delete()}

Open in new window

0
SubsunCommented:
Try..
$username=Read-host "Please Enter Username: "

$PServers="CIEH-RDS01","CIEH-RDS02","CIEH-RDS03","cieh-fnp01"

$Pservers | foreach {(Get-WmiObject Win32_UserProfile -ComputerName $_ | Where {$_.LocalPath -like "*\$username*"}).delete()}

Open in new window

Just added -ComputerName parameter to Get-WmiObject
0
chglAuthor Commented:
Hi subsun,

This doesn't, I think we need to take ownership of the folder before deleting.

thanks for you help :)
0
SubsunCommented:
Are you running the code with a account which has permission to delete the profiles?
0
chglAuthor Commented:
Yes, but normally when deleting manuall we have to take ownership.
0
chglAuthor Commented:
Hi Guys this code below works fine:

$username="a.test"

$PServers="server-RDS01","server-RDS02","server-RDS03"

$Pservers | foreach {(Get-WmiObject Win32_UserProfile -Computername $_ | Where {$_.LocalPath -like "c:\users\$username"}).Delete()}

Open in new window


however when i try to add Read-Host "Please enter the username " on $username it fails

Also I need to delete a folder on our file and print server which contains the users roaming profile. this script deletes the local profiles on the servers.

Also I need to do a write host to tell me that is has been deleted off a server. How can I do this?

Thank you advance.
0
SubsunCommented:
$ErrorActionPreference = "Stop"
$username= Read-Host "Input Username"
$PServers="server-RDS01","server-RDS02","server-RDS03"

$Pservers | foreach {
$Srv = $_
	Try{

	(Get-WmiObject Win32_UserProfile -Computername $Srv | Where {$_.LocalPath -like "c:\users\$username"}).Delete()

	Write-Host "Deleted profile of $username from $Srv"
	}
	Catch{
	Write-Host "Error For $username from $Srv - Error $($_.Exception.Message)"
	}
}

Open in new window


If you are copy pasting the code to PowerShell console then read-host won’t work..  you need to run it from any script editor like ISE.. or save it as a .ps1 file and then run it as a script..

Also I need to delete a folder on our file and print server which contains the users roaming profile. this script deletes the local profiles on the servers.
How do you identify the path to this folder? does it have a speciific format with match with user name? or read it from AD account User Profile Path?

How do you identify the path to this folder? does it have a specific format with match with user name like \\server\temp\$username? or read it from AD account User Profile Path?
0
chglAuthor Commented:
I have written this script and it works! Thanks Subsun, i have learnt the try and catch method from u. Now I need to add one more line and the script is almost complete,

the roaming profile is stored in \\s-fnp01\rdsprofiles$ -

$users=@("c.test")

$PServers=@("S-RDS01","CS-RDS02","S-RDS03")

foreach ($server in $PServers)

{





	foreach ($user in $users)

		
		{

		try 
			{ 


			(Get-WmiObject Win32_UserProfile -Computername $server | Where {$_.LocalPath -like "c:\users\$user"}).Delete()	
			

				Write-Host "$user deleted successfully on $server" 
			}
			
			
		catch

			{

				Write-Host "Error For $username from $Server - Error $($_.Exception.Message)"


			}

		}


}

Open in new window

0
SubsunCommented:
Let’s say of the folders naming is same as user name which you input the.. you can add the following line just after you delete user profile, this will delete the roaming profile too..

Remove-Item "\\s-fnp01\rdsprofiles$\$user" -Recurse -Force 

Open in new window

0
chglAuthor Commented:
theres .v2 extension on all the profiles:

v2 extension
0
chglAuthor Commented:
also there we if i was to delete manually, i need to take owenership even though i am the administrator, will the -force in powershell script insist of deleting this folder even though owenership has not bee taken
0
SubsunCommented:
I would suggest you to add a control like following... Else if the $user value is null the code will delete the other folders from \\s-fnp01\rdsprofiles$\

If ($user -ne $null){
Remove-Item "\\s-fnp01\rdsprofiles$\$user" -Recurse -Force
}

Open in new window

0
SubsunCommented:
You can try to use takeown or icacls to take ownership of the folders.. Try this and see if it works for you..

If ($user -ne $null){
$Folder "\\s-fnp01\rdsprofiles$\$user.V2"
takeown /f $Folder /r /d y
Remove-Item $Folder -Recurse -Force
}

Open in new window

0
chglAuthor Commented:
how will i incorporte this into my script, becuase theres a foreach on the script and it will try more then once,
0
SubsunCommented:
You can add above code after line 22 in your script or you can create a separate code for to delete roaming profile..

Here is updated code.. it will check the path before it try to delete it..
If ($user -ne $null){
$Folder = "\\s-fnp01\rdsprofiles$\$user.V2"
	If (Test-path $Folder){
	takeown /f $Folder /r /d y
	Remove-Item $Folder -Recurse -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
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.