loops in Powershell - powershell

I'm not so sure how to do loops in Powershell.
get-aduser -Filter {name -like "USER"} >C:\temp.txt
I have a list of 200 users (I have first and last name) specific that I would like the logon name to same file how can I do that with the command a I wrote before? Or is there any other way to did it ?

Use a foreach loop like #msanford said and loop through your user list with something like this:
get-aduser -Filter {Name -like "USER*"} | Select-Object Name,SamAccountName | Out-File -FilePath C:\temp.txt

Related

How to get the get-ADPrincipalGroupMembership for all users in a txt or csv file and put into a txt file for each user?

I am trying to get a file with the group-memberships for every user that is specified in a txt/csv file.
so this is what i had before:
Get-ADPrincipalGroupMembership -Identity $user -Server $DC | Select name | Where-Object name -like GUSR_* | Out-File "C:\temp\$user.txt"
this work fine for getting the groups from 1 singel user, but now i have to do this for 100+ users.
And instead of doing it one by one i am looking for a way to automate it.
so i got myself a .csv export of all the users i want this done for.
and started trying.
what i came up with so far:
$users = Get-Content "C:\temp\test.csv" |ForEach-Object {Get-ADPrincipalGroupMembership -Identity $users -Server $DC | Select name | Where-Object name -like GUSR_* | Out-File "\\ads.net\ADS\SDL\Temp\_ROLAND\RSD\test2\$users.txt"}
This cleary doesnt work.
I have tried a couple of other things with the foreach command but nothing did the trick.
I have the feeling i am not on the right path to get my result.
Maby somebody has done this before and can help me get on the right path.
i'm not new to powershell but i'm far from an expert, most of the time i use it for basic singel commands or edit some great scripts i find.
sadly for this i haven't found any yet.
with kind regards
Roland
Don't assign back to a variable
Import the CSV
No filter after select
Pretiffy your -like
Use $_ as pipeline variable
Use subexpression operator for string+variable concatenation
Import-Csv "C:\temp\test.csv" |ForEach-Object {Get-ADPrincipalGroupMembership -Identity $_.users -Server $_.DC | Where-Object {$_.name -like 'GUSR_*'} | Select -Expand Name | Out-String | Out-File "\\ads.net\ADS\SDL\Temp\_ROLAND\RSD\test2\$($_.users).txt"}

exporting AD users displayName for selected groups only - powershell

I am new to powershell so please excuse me if the answer is quite simple. I am trying to get user list sorted by selected AD groups and export that to table or csv at least. Due to the fact that:
Get-ADGroupMember -Identity "TestGroupName"
... gives me only user IDs for my AD, I used below:
Get-ADGroupMember -Identity "TestGroupName" | Get-ADObject -Properties displayName
This works perfectly but I do not want to type manually each group there so I decided to first export groups that I need which are beginning with "Test":
Get-ADGroup -Filter "name -like 'Test*'" |Select-Object Name | Export-csv -path \Groups.csv
Now I want to use information from Groups.csv to list all user displayName for groups listed in Groups.csv so I tried something like that:
Import-Csv -Path .\Groups.csv | Get-ADGroupMember ForEach($Name in $Groups) | Get-ADObject -Properties displayName | Export-csv -path \UsersByGroups.csv
unfortunately it does not work properly maybe because I still do not get exactly how to use ForEach
Can someone with more experience have a look and help?
Thanks!
Maciej
Just pipe the groups output by Get-ADGroup -Filter ... directly to Get-ADGroupMember:
Get-ADGroup -Filter "name -like 'Test*'" |Get-ADGroupMember |Get-ADObject -Properties displayName

cmdlet to variable not being accepted

Should be a very simple script but having issues getting the output from the get-aduser to be recognized as a variable, among other things. I've tried every format of quotes and brackets I can think of but can't get a proper output. The script is just querying a specific user and exporting the AD groups to a folder named for their department, then into a text file using the name and title.
$usertocheck = Read-Host -Prompt 'Input user to check'
$depttoadd = Get-AdUser -Filter {samAccountName -eq "$usertocheck"} -Properties Department |
Select-Object -expand Department
New-Item -ItemType Directory -Force -Path "C:\Users\Public\Desktop\UserRecords\$depttoadd\"
Get-ADPrincipalGroupMembership $usertocheck | select name |
Out-File -FilePath "C:\Users\Public\Desktop\UserRecords\$($usertocheck)_$($titlelookup).txt"
Any hints would be appreciated.
It works for me, when I remove the quotes around $usertocheck in the below line ($usertocheck is a string already, so no need for quotes)
$depttoadd = Get-AdUser -Filter {samAccountName -eq $usertocheck} -Properties Department |
As a side note, you could also access the department property of the object returned by Get-AdUser like so
$depttoadd = $(Get-AdUser -Filter {samAccountName -eq $usertocheck} -Properties Department).Department
Acessing the properties of an object is from my experience the more reliable and cleaner way of getting the output you want, rather than using 'Select-Object'.
Hope this helps.

How to retrieve only enabled users from the Active Directory

I'm trying to retrieve only enabled users in the AD. When I run this code line it returns the error. I tried using a filter as well to filter only enabled users for the requested info but it returns ALL users from every domain instead of just the single id.
Get-ADUser : A positional parameter cannot be found that accepts argument 'enabled -eq 'true''.
This is my code that is throwing the error.
Get-ADGroupMember -Identity 'Animal Shop A' | Get-ADUser -Filter '*' | Get-ADUser Where "enabled -eq 'true'" | Get-ADUser -Properties ('Mail')
This one returns ALL users from every domain
Get-ADGroupMember -Identity 'Animal Shop A' | Get-ADUser -Filter "enabled -eq'true'" | Get-ADUser -Properties ('Mail')
Is my syntax wrong on both of them? If I just want to return values from say "Animal shop A" and then "Animal Shop B"
.. or a little bit shorter this way:
Get-ADUser -Filter 'enabled -eq $true' -Properties mail |
Select-Object -Property Name,samaccountname,mail
Besides this I would recommend to use a -SearchBase. That's less stressful for the AD. ;-)
Get-ADUser -Filter * -Properties mail | Where { $_.Enabled -eq $True} | Select Name,samaccountname,mail
That will get all enabled users in your target domain that are enabled and display the name, username, and mail properties
Important to know for both commands:
You must work with an elevated powershell process.
Otherwise the result may not be complete.
get-aduser -filter 'enabled -eq "true"' -ResultSetSize $Null
simply try below commands in powershell as administrator permission.
As a guide, the first part will filter users, second part filtered enabled users and last part will give you export of results.
Get-ADUser -Filter * -Property Enabled | Where-Object {$_.Enabled -like “false”} | Export-Csv -Path C:\eport.csv -Encoding ascii -NoTypeInformation
hope to be useful for you.

Powershell ActiveDirectory module Variable with wildcard not working

Why doesn't the below get-adcomputer commandline return any results? It's really irritating when cmdlets don't accomodate powershell syntax. At least that's what it looks like is happening here. If I do a write-output, it displays exactly what I want to be inserted in the commandline. However, when I go to use it with the get-adcomputer cmdlet, no results are returned.
PS: C:\> $Variable = "88FF"
PS: C:\> write-output "*$($Variable)*"
*88FF*
PS: C:\> Get-ADComputer -Filter {Name -like "*$($Variable)*"} -Property *
PS: C:\>
PS: C:\> Get-ADComputer -Filter {Name -like "*88FF*"} -Property *
computer1
computer2
computer3
I've tried a bunch of different variants... including even adding literal quotes to the variable by escaping them. I've been pulling my hair out trying to figure out something that should take less than 10 seconds to do.
PS: C:\> $Variable = "`"*888FF*`""
PS: C:\> $Variable
"*88FF*"
PS: C:\> PS: C:\> Get-ADComputer -Filter {Name -like $Variable} -Property *
PS: C:\>
Edit: I've also tried below variant with same exact result:
PS: C:\> Get-ADComputer -Filter {Name -like '*$Variable*'} -Property *
PS: C:\>
Give this a try:
Get-ADComputer -Filter "Name -like '*$Variable*'" -Property *
Pretty lame, it looks like this is one of the many limitations of the Powershell Active-Directory module that comes with Windows. I wasn't doing anything wrong in my original attempts. I ended up having pipeline the output to where{ } to filter it items.
Get-ADComputer -properties Name, OperatingSystem -Filter *| ?{$_.name -like "*$($Variable)*"} |ft Name, OperatingSystem -Wrap -Auto
How about this:
$myvar="*888FF*"
get-adcomputer -filter {name -like $myvar} -property *
It's really annoying, you would expect this to work but -filter just has some weird parsing rules internally I guess
$myvar="888FF"
get-adcomputer -filter {name -like "*$myvar*"} -property *
Shay's solution works beautifully (at least on v4)
I also found out that LDAPFilters will work too!
get-adcomputer -LDAPFilter "(&(name=$name*)(operatingsystem=server))"