Power Shell script to delete the User profiles from the windows server 2008 r2 to local machine(Windows 7)

0

I tried the following script on a Windows Server 2008 R2 to delete the User Profiles on a Windows 7 client machine.

PS C:\> get-content localcomputer-list.txt | get-userprofile | where {$_.LastUse 'lt (Get-Date).AddDays(-90)}} | remove-userprofile -whatif

It doesn't work.

How do I resolve this?

kunal

Posted 2015-07-21T18:29:51.937

Reputation: 101

What are the results after running this? Are you getting any errors? Where does it fail to run? – paradd0x – 2015-07-21T18:59:55.587

it doesn't return anything and and even command doesn't run completely – kunal – 2015-07-21T19:15:54.970

after ruuning it. i get >>.

when i run this command let Get-userporfile manully , i get the error cmdlet is not recozinized – kunal – 2015-07-21T19:17:18.990

If you get >> that is new line in Powershell console, it means that you didn't close a string somewhere or there is a pipe | that has nothing on the right side (console thinks that there's something more to add). Btw. it should be -lt not 'lt and you have double bracket after AddDays(-90). My guess it's 'lt that causes it. – mwilczynski – 2015-07-21T23:32:37.440

I use DelProf2 for this kind of task, just deleting the files usually doesn't work.

– Peter Hahndorf – 2015-07-22T11:40:17.603

are you using DelProf2 in your production environment ? can we automated the Task – kunal – 2015-07-22T13:39:01.637

Answers

1

I assume you got your snippet from here:

http://www.itninja.com/blog/view/manage-purge-local-windows-user-profiles

You need to load the get-userprofile modules.

Also, buddy uses ' to denote - in his code. So:

get-content mydesktops.txt | get-userprofile | where {$_.LastUse -lt (Get-Date).AddDays(-90)} | remove-userprofile

Script doesn't work for me if I try to use it remotely as indicated in his post.

Ergo this works:

get-userprofile | where {$_.LastUse -lt (Get-Date).AddDays(-90)} | remove-userprofile

Dilborg

Posted 2015-07-21T18:29:51.937

Reputation: 11