loop through all user IDs??

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

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

foreach $($.name) # each domain user ID (possible to specify only users in Domain Users group??)
{
# do code e.g.
Set-Location “\$server\C$\Users\$_.name\AppData\Local\Temp&quot; -ErrorAction SilentlyContinue<br>} # end foreach<br><br>Thank you, Tom</blockquote>by mjolinor at 2013-03-01 08:03:12<blockquote><code>$users = &lt;command that gets a list of user names&gt;<br><br>foreach &#40;$user in $users&#41;<br> {<br> # do code e.g.<br> Set-Location &quot;\\$server\C$\Users$user\AppData\Local\Temp” -ErrorAction SilentlyContinue
}

See get-help about_foreach

Or use the pipeline with foreach-object:

$user = <command that gets a list of user names>

$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!! :slight_smile: Tom