Author Posts

January 1, 2012 at 12:00 am

by tommls at 2013-03-01 07:50:28

Can I do this?? Or should it be done differently??

foreach $($ # each domain user ID (possible to specify only users in Domain Users group??)
# do code e.g.
Set-Location "\\$server\C`$\Users\$\AppData\Local\Temp" -ErrorAction SilentlyContinue
} # end foreach

Thank you, Tom

by mjolinor at 2013-03-01 08:03:12

$users =

foreach ($user in $users)
# do code e.g.
Set-Location "\\$server\C`$\Users\$user\AppData\Local\Temp" -ErrorAction SilentlyContinue

See get-help about_foreach

Or use the pipeline with foreach-object:

$user =

$users |
foreach-object {
# do code e.g.
Set-Location "\\$server\C`$\Users\$_\AppData\Local\Temp" -ErrorAction SilentlyContinue

Note the difference in the way the user object is referenced ($_ in the pipeline, and $user in the foreach loop)

by tommls at 2013-03-01 08:05:27

What command gets the user's ID (aka SAM Account Name)??
I'm now trying to figure this out..
Thank you, Tom

by mjolinor at 2013-03-01 08:10:01

If you have the RSAT tools installed (with the ActiveDirectory module)

Get-ADUser | select -ExpandProperty sAMAccountName

by tommls at 2013-03-01 08:24:29

That doesn't work because it doesn't get ALL users and requests a filter which I tried including...
e.g. Get-ADUser | select -ExpandProperty sAMAccountName -Filter * -SearchBase "OU=Domain Users,DC=foo,DC=local" does not work but it is the gist of what I want...It absolutely must select only Domain Users.
This command must be set equal to $users to create an array of user IDs.

by mjolinor at 2013-03-01 08:48:19

$Users = Get-ADUser -Filter * -SearchBase "OU=Domain Users,DC=foo,DC=local" |
select -ExpandProperty SamAccountName

by tommls at 2013-03-01 09:20:53

I had tried that command but I now realize I had the filter/search and search reversed because I did not fully understand the order of things in the docs.
Thank you!! 🙂 Tom