Thursday 4 February 2010

Remove-Item

This is just going to be a quick post, it’s a quick bit about what I did earlier at work.  As a bit of background, we have lots of users logging onto lots of computers and profiles don’t currently exist in the roaming variety.  At times the profiles that create on the desktops around the place mess up and have to be deleted.  Doing this manually claims it takes between hours and days to complete.  Before Powershell I made a .bat file that had a list of usernames prefixed by rmdir.  Earlier I had to do the task again and I thought, how will I do this in powershell?

As a start I just went to the Documents and Settings and typed “Remove-Item .\04* –force” (for student users, usernames are prefixed with year of entry), then I though, Can I do more than one at a time so I tried “Remove-Item .\05*,.\06* –force”.

After being happy with that I thought, ‘what more can I do?’, so after “get-help remove-item” I noticed an exclude option, that got me thinking.  All I want to keep in is “All Users”,“Default User”, and any users starting with admin.  I ended up with the script as follows:

c:

cd '\documents and settings'

Remove-Item .\* –Exclude “All Users”,”Default User”,Admin* -force

That’s all there was to deleting everything but what I wanted in that directory – also completes quickly – no waiting for a few hours for the deletes to go though.

This is such a basic task made better with powershell.  Whatever you are trying to do, think, What Can Powershell Do?

Quick warning: DO NOT type “Remove-Item \*” without the . \ for current directory or you may wipe the c: drive

1 comment:

  1. Nice one, I'm going to mod that to keep my home folders clean. Thanks

    ReplyDelete