I need to extract information using a powershell cmdlet and a txt file.
The TXT file contains a list of groups
I want to first feed powershell the script... pretty simple:
get-content c:\scripts\mygroups.txt
I then want to run a Foreach-object cmdlet against it and pull only the distinguished name
The problem is that I keep running into the -Filter command and I shouldn't need the filter command because the names are exactly pulled from AD.
Foreach-Object {Get-ADGroup -Filter "*" | select DistinguishedName} works but I dont want all the groups I want the variable that I used for the get-content command. I feel I am missing some type of link between the -Filter and selecting the field I want to display. Please help me link the two together. Thanks!
Here is the error I am getting...
Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADGroup
Assuming that each group name is on a line in the file and there are no blank lines, try this:
Get-Content c:\scripts\mygroups.txt | Foreach {Get-ADGroup $_} |
Select DistinguishedName
You could actually take out the "Foreach" part of Keith's code and just let the pipeline do the loop for you:
Get-Content c:\scripts\mygroups.txt | Get-ADGroup | Select DistinguishedName
This is still assuming that the text file contains the group names, ("Name" attribute), with only one group name per line.
Pipe the content of the file to the Get-ADGroup cmdlet and expand the DistinguishedName of each output object:
Get-Content c:\scripts\mygroups.txt |
Get-ADGroup |
Select-Object -ExpandProperty DistinguishedName
Related
So, with the same code from my last question I've got a new problem. It returns a count, but there's two AD groups causing problems. We'll call them 'East Group' and 'West Group'. Both written exactly that way with spaces and thus requiring quotes. When I run:
(Get-ADGroup "East Group" -Properties *).member.count
It returns the count of users no problem. However, when I run the code for my grand total ignoring duplicates:
$script:cnt = 0
$Groups = Get-Content -Path $someFile
$Groups | Get-ADGroupMember | Select-Object -expand DistinguishedName -Unique | ForEach-Object { $script:cnt++ }
$script:cnt
It returns a total, but also an error saying that it cannot find West Group or East Group under my domain. My best guess is it's somehow ignoring the quotation marks in the text file. Is there a way to make it read it as "East Group" or some other workaround?
it's somehow ignoring the quotation marks in the text file.
Don't put quotation marks in your plain-text file with group names, if you're reading it with Get-Content - such quotes will become part of the values, which is not your intent - simply rely on Get-Content to read the file line by line, which will work correctly even with values with spaces.
# Create the group-list file - do NOT use quotation marks around the entries.
#'
NoSpacesGroup
East Group
'# > Groups.txt
# Demonstrate that each group name is read correctly, even if it
# contains spaces.
Get-Content Groups.txt | ForEach-Object { "this group: >>$_<<" }
The above yields:
this group: >>NoSpacesGroup<<
this group: >>East Group<<
demonstrating that even the value with spaces was correctly read as a single value (line).
Therefore, simply remove the quotes from your group file and try your command again.
Basically what's happening is that I have two files, one with a list of allowed users and another list of users that are actually on the desktop. What I'm trying to do is use a for loop to grab each name in the AllowedUsers.txt file, and use Select-String to find any names that match and delete them from the original Users.txt (the list of users actually on the desktop). The end goal is to have a list of users that aren't allowed on the desktop left in the Users.txt file, where I can then for loop through once again and simply delete those users through command line.
Unfortunately, I have been unable to get this to work as I don't fully grasp how Select-String or for loops work, but any help writing the script would be greatly appreciated.
Example for AllowedUsers.txt (Input):
abbby
Sebastian
Evan
Example for Users.txt (Input):
abbby
Evan
Sebastian
Ethan
zachary
Example for edited Users.txt (Desired Output):
Ethan
zachary
here's another way to do it. unlike the solution by LotPings, it uses the array membership operator -notin to see if the items in the general user list are in the allowed list. i did not overwrite the the source user list since that bothers me. [grin] you can easily change that, tho.
note that PoSh versions below 3 will need to use -notcontains and swap the items to put the collection on the left of the test instead of the right. thanks to Theo for the reminder about that!
# fake reading in a text file
# in real life, use Get-Content
$AllowedUsers = #'
abbby
Sebastian
Evan
'# -split [environment]::NewLine
# another fake file read
$AllUsers = #'
abbby
Evan
Sebastian
Ethan
zachary
'# -split [environment]::NewLine
# this presumes the source files are both text and have the same name format
# the "-notin" operator is NOT case sensitive
$ExcludedUsers = $AllUsers |
Where-Object {$_ -notin $AllowedUsers}
# send to screen
$ExcludedUsers
# send to text file
$ExcludedUsers |
Set-Content -LiteralPath "$env:TEMP\BassoftheC_ExcludedUserList.txt"
on screen ...
Ethan
zachary
text file content ...
Ethan
zachary
I am running multiple scripts in PowerShell ISE to allow me to remove a specific line of code in many XML documents. The code I have been advised to use is the following:
Get-Content .\AdamInfTest.xml | Get-Content .\AdamInfTest.xml | Where-Object {$_ -notmatch '<!DOCTYPE'} | Set-Content .\Complete\AdamInfTestOut.xml
This will remove the line of code in the XML file with the text 'DOCTYPE' and this does work. However, when running the script the outputted file is replicating the entire document code over and over until the file becomes worryingly large (about 30MB when the non-scripted XML is about 60KB).
I am unsure why this is happening and any help would be appreciated - Thanks!
This Get-Content .\AdamInfTest.xml | Get-Content .\AdamInfTest.xml doesn't make sanse as this will mean that you re-read the whole .\AdamInfTest.xml for each line in the .\AdamInfTest.xml file. I guess that 60Kb times the number of lines in the .\AdamInfTest.xml (which I do not know) is about 30Mb... In other word what happens when you remove one of the Get-Content .\AdamInfTest.xml? –
In PowerShell, I would like to get members of a group name in one line separated by semicolon or simply in one line output.
PS C:\Windows\ Get-ADGroupMember -Identity "Hardware Drawing" | Select Name
Name
----
Rusten, Brian
Kim, Calvin
I want above names in one line separated by semicolon or simply in one line
You probably need to use the -expand switch of the Select-Object cmdlet. Then use the -join operator. See also about_join:
(Get-ADGroupMember -Identity "Hardware Drawing" | Select -expand Name) -join ';'
Just wondering if you can help me out.. I am trying to compare two list(txt file) and find strings that are in list A and not in List B and output it to another txt file.. anybody know how to do it using powershell ?
Here is what I have so far:
Compare-Object -ReferenceObject $FolderLists -DifferenceObject $AdUserName -passThru
I would like to find all strings that are in $FolderLists and not $AdUserName and possibly output it to another variable. The issue I am having is that it outputs strings that are not in both lists.
I assume $FolderList and $AdUserName are arrays of strings? You don't really need Compare-Object to compare arrays. It's as simple as this:
$FolderList | ?{$AdUserName -notcontains $_}
Compare-Object is for comparing the specified properties of collections of objects with common properties. You could do this with Compare-Object if you really want, like this:
Compare-Object $FolderList $AdUserName | ?{$_.SideIndicator -eq '<='} | Select-Object -ExpandProperty InputObject
But as you can see, it's overkill for this task.
To output the result to another variable, simply assign it:
$AnotherVariable = $FolderList | ?{$AdUserName -notcontains $_}