Link to home
Start Free TrialLog in
Avatar of chgl
chglFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of SubSun
SubSun
Flag of India image

Avatar of chgl

ASKER

thanks, i have founds this:

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


however will this also remove the SID from the registry?
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/
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..
Avatar of chgl

ASKER

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

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
Avatar of chgl

ASKER

Hi subsun,

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

thanks for you help :)
Are you running the code with a account which has permission to delete the profiles?
Avatar of chgl

ASKER

Yes, but normally when deleting manuall we have to take ownership.
Avatar of chgl

ASKER

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.
$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?
Avatar of chgl

ASKER

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

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

Avatar of chgl

ASKER

theres .v2 extension on all the profiles:

User generated image
Avatar of chgl

ASKER

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
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

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

Avatar of chgl

ASKER

how will i incorporte this into my script, becuase theres a foreach on the script and it will try more then once,
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial