r/PowerShell • • Dec 04 '23

Script to delete specified folders with input

Hey all.

We have user profiles roaming across various network shares for our virtual users, depending on their geographic location. For example:

\\share1\username\

\\share2\username\

\\share3\username\

I've been trying to figure out a script for our desktop support team so they can run it, get asked for the username and upon providing the name have it purge that profile folders at each share as shown above. This way, I don't have to rely on them remembering to clean things up at each location.

Any suggestions?

0 Upvotes

12 comments sorted by

15

u/Sunsparc Dec 04 '23

What have you tried so far? This should actually be pretty easy.

10

u/McAUTS Dec 04 '23

Do you know PowerShell?

I mean that's basically reading a Prompt for User Input and then concatenate this input with the corresponding shared path string. Then you delete it.

Where you stuck?

12

u/Stoneteer Dec 04 '23

OP hasn't tried anything

9

u/[deleted] Dec 04 '23

Just a note, if you ever make a script and hand it off to someone to use, ensure the script can handle input goofs.

4

u/aidansdad22 Dec 05 '23

yes!
You're going to want to take whatever input (username) is entered and actually test the path and if it doesn't exists report that "that path doesn't exist. Please check your input and try again" and then ask for input again until they get it right.

1

u/BlackV Dec 05 '23

better still parameterize the script, make it mandatory and run validatepath on the values

0

u/Luc-e Dec 04 '23 edited Dec 05 '23
$username = read-host „Please enter username: „


$shares = „share1“,“share2“,“share“

Foreach ($share in $shares){remove-item -path „\\“ + $share + „\“ + $username -recurse -force}

Something like that? Just wrote out of my mind on phone so please check commands lol

1

u/BlackV Dec 05 '23
  • whats in gods name is happening with your quotes?

  • formatting 4 spaces in front of you code lines

for example

<4 sapces>$shares = "share1","share2","share"

1

u/Luc-e Dec 05 '23

Ah thx didn’t know that

1

u/BlackV Dec 05 '23

good as gold

-1

u/DeusExMaChino Dec 05 '23

Use DFS with server target prioritization instead of that awful network share design

2

u/BlackV Dec 05 '23

yes DFS is a better way here