-1

I have tried searching for this, but I do not think I am using the correct words.

I am creating a script that needs to work on many environments. One of the specifications, is that I need to be able to delete a certain directory in a users AppData. Problem with it, is I do not know how to set a dynamic path.

I.e. C:\Users\User1\AppData\Local\X compared to C:\Users\User2\AppData\Local\X

How would I get and specify a series of user accounts on the local machine, ideally with out polling AD?

Yu Zhang
  • 2,037
  • 6
  • 28
  • 42
Moridn
  • 35
  • 6
  • I'm not sure I understand the question. Can you show the relevant code where you need to use this? – Etan Reisner Oct 26 '15 at 21:33
  • @EtanReisner I don't have anything yet, heh. I am thinking on how I will write this out before actually doing it. I have a citrix enviroment. I need to delete files in a series of directories, but each one is in a different users folder. Other than the name of the account in the path, each directory is identical. Almost like the account name is a variable I could pass to the delete line. – Moridn Oct 26 '15 at 22:10
  • You should try writing something. And looking up how to format/concatenate strings/etc. in powershell. – Etan Reisner Oct 26 '15 at 22:12

2 Answers2

0

Check out the environment variables for paths to local resources using Get-ChildItem like so:

Get-ChildItem -Path env:

This will show you all environment variables and their value without the need to query Active Directory, the one you want for AppData\Local is called LOCALAPPDATA

To use an environmental variable in a function the syntax is $ENV:<Name> so to use the environmental variable for LOCALAPPDATA you would use $ENV:LOCALAPPDATA

Play around with the environmental variables and start coding your script, if you have additional questions you can then post your script and we can contribute a more specific answer to help you out :)

Bluecakes
  • 2,069
  • 17
  • 23
0

To get user names from AD, AD module must be installed on the system from which you run the queries.

#Export to a csv file user names and use it as the source.
Note that this command will export all users from your AD.

    Get-ADuser -filter * | select name | Export-Csv c:\users.csv
    $users = Import-Csv c:\users.csv

    Foreach ($user in $users.name) {

    $path = "c:\users\$user\appdata\local\X"
    if ( $(Try { Test-Path $path.trim()} Catch { $false }) ) {

        Remove-Item $path -Force -Recurse
     }
    Else {

       write-host "Path not found"
     }

    }
kekimian
  • 909
  • 3
  • 13
  • 21