get group members separated by semicolon or only in same line - windows

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 ';'

Related

How to remove duplicates lines from text file while keeping order of file

I have a text file with 105,779 lines containing duplicates. I need to remove the duplicates without changing the order. I am able to remove the duplicates, but it is changing the order of the text file. Any help would be appreciated.
The Select-Object cmdlet has a -Unique switch that does just what you want:
Get-Content allMethods.txt | Select-Object -Unique > uniqueMethods.txt
Note (written as of PowerShell 7.2.3):
While Select-Object -Unique is surprisingly slow, it still outperforms your own manual solution.
The inefficient implementation is the subject of GitHub issue #11221.
Unlike PowerShell in general, -Unique is surprisingly and invariably case-sensitive:
'a', 'a', 'b' | Select-Object -Unique # -> 'a', 'b'
'a', 'A', 'b' | Select-Object -Unique # !! -> 'a', 'A', 'b'
See GitHub issue #12059 for a discussion of this unexpected behavior. (The expected behavior would be to case-insensitive by default and offer a case-sensitive opt-in).

Powershell issue reading groups from txt file

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.

Compare two list and find names that are in list one and not list two using powershell

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 $_}

Looking to extract information using powershell

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

VS2010 Post-build event, replace string in a file. Powershell?

I need to replace a simple string in a minified .js file after a successful build in VS2010.
So I'm trying to run a simple command line call from the Post-build events window.
This example, from here: https://blogs.technet.com/b/heyscriptingguy/archive/2008/01/17/how-can-i-use-windows-powershell-to-replace-characters-in-a-text-file.aspx totally mangulates the resulting .js file. Something is wrong, I suspect it is coming across some weird chars in my minified .js file that screws it up.
(Get-Content C:\Scripts\Test.js) |
Foreach-Object {$_ -replace "// Old JS comment", "// New JS comment"} |
Set-Content C:\Scripts\Test.js
How can I achieve such a simple task like I could do in unix in a single line..?
It would be great to see the diff file. Without more info, some info:
Set-Content adds a new empty line at the end (probably not a problem for you)
You can use -replace operator like this:
(gc C:\Scripts\Test.js) -replace 'a','b' | sc C:\Scripts\Test.js
-replace works on arrays too.
You could read the content via [io.file]::ReadAllText('c:\scripts\test.js') and use-replace`, but again, I don't think there will be significant difference.
Edit:
Double quotes are used when evaluating the string. Example:
$r = 'x'
$a = 'test'
'beg',1,2,"3x",'4xfour','last' -replace "1|$r","$a"
gives
beg
test
2
3test
4testfour
anything
To save the content with no ending new line, just use [io.file]::WriteAllText
$repl = (gc C:\Scripts\Test.js) -replace 'a','b' -join "`r`n"
[io.file]::WriteAllText('c:\scripts\test.js', $repl)

Resources