Formatting variable with 2 outputs - powershell

Code:
$adgroups = Get-ADPrincipalGroupMembership $tag$ | select -ExpandProperty Name | Sort | Select-String "iSite"
Output:
DFSR Managed iSite Enterprise 4.4.542.2 WSA_Rad_A
DFSR Managed iSite Radiology 4.4.516.27 WSA_Rad_A
Basically one command generates two items (output using $variable | out-file C:\file.txt -Append) and when I go to open these in excel they format as one line like this:
DFSR Managed iSite Enterprise 4.4.542.2 WSA_Rad_A DFSR Managed iSite Radiology 4.4.516.27 WSA_Rad_A
Is there a way to break it up // add a new line after each item but still keep them both inside one variable?

Get-ADPrincipalGroupMembership $tag$ | select -ExpandProperty Name | Sort | Select-String "iSite" | ConvertTo-Csv -NoTypeInformation | Out-File C~\Desktop\Sites.csv

I would break up your request a bit.
First, you're using Select -Expand, which is going to discard all of the properties and only return the values for each object's Name. This is a problem because when you export it as a CSV, you're not going to have a heading. I think the lack of header is ultimately leading to the issue you're facing here.
Try this instead:
$adgroups = Get-ADPrincipalGroupMembership $tag$ | Where Name -like "*iSite*" |
select Name | Export-Csv c:\pathto\YourCsv.Csv
Finally, I don't think Select-String is doing you any favors. You could instead use the -like operator.

Related

Sort CSV powershell script delete duplicate, keep the one with a special value in 3rd column

How do I delete double entriys in a csv by one column and leave the one with one special value in one of the columns?
Example: I got a csv with
Name;Employeenumber;Accessrights
Max;123456;ReadOnly
Berta;133556;Write
Jhonny;161771;ReadOnly
Max;123456;Write
I want to end up with:
Name;Employeenumber;Accessrights
Max;123456;Write
Berta;133556;Write
Jhonny;161771;ReadOnly
I tried by Get-Content Select-Object -unique, but that does not solve the problem that it should only keep the ones with the value "write" at the property Accessrights.
So I have no clue at all
You can use a combination of sorting and grouping ....
#'
Name;Employeenumber;Accessrights
Max;123456;ReadOnly
Berta;133556;Write
Jhonny;161771;ReadOnly
Max;123456;Write
'# |
ConvertFrom-Csv -Delimiter ';' |
Sort-Object -Property Name, Accessrights -Descending |
Group-Object -Property Name |
ForEach-Object {
$_.Group[0]
}

Select-Object - Keep default columns but add one

I can use Select-Object to choose which columns to show and even add calculated columns. An example:
gci | select *, #{n='LAS'; e={(Get-Date)-$_.LastAccessTime}}
I want add a calculated column but keep the defaut ones. Without the * wildcard I only get my calculated property. With it I get everything. The only workaround I've got to work is to manually list the default property names. Any ideas?
The thing is that that you actually states to display all properties ('*').
So to add to only the standard properties, you first need to get the standard properties.
[string[]]$StdProperties = (Get-ChildItem).PSStandardMembers.DefaultDisplayPropertySet[1].ReferencedPropertyNames
We don't actually want to change the standard property of the objects returned
Get-Childitem | select Name | Get-Member| group TypeName | select Name
Name
----
Selected.System.IO.DirectoryInfo
Selected.System.IO.FileInfo
So we just need to expand on that extracted string array with the new property to use.
$StdProperties += 'LAS'
And finally, to put it to use...
Get-ChildItem | select *, #{n='LAS'; e={(Get-Date) - $_.LastAccessTime}} |
select $StdProperties
Just for fun, building on Abrahams comment, you could do something weird like this:
# get the default properties used on Format-Table
$defaultProps = (((Get-ChildItem | Format-Table | Out-String) -split '\r?\n' |
Where-Object { $_ -match '^\w.*' }) |
Select-Object -First 1) -split '\s+' -ne ''
# now execute the command
Get-ChildItem | Select-Object *, #{n='LAS'; e={(Get-Date)-$_.LastAccessTime}} |
Select-Object ($defaultProps + 'LAS') | Format-Table -AutoSize

Need to split (cutout a part of) the result of a powershell command into a CSV

I have a command that spits outs Active Directory SamAccountName, Description, Distinguished name into a CSV. My issue is that I only need the part of the distinguished name after the user name. I know this is conceptually simple, but I don't know enough about using Splits to get the results.
Here is my command:
get-content users.txt | get-aduser -pr
SamAccountName,Description,DistinguishedName | select
SamAccountName,Description,DistinguishedName | Export-Csv users.csv
Here is my output:
Line1-- "user1","user1 description","CN=Lastname\, Firstname(user1),OU=OU2,OU=OU1,OU=Site-1,OU=Accounts_User,DC=domain,DC=company,DC=com"
Line2--- "user2","user2 description","CN=Lastname\, Firstname,OU=OU1,OU=Site-2,OU=Accounts_User,DC=domain,DC=company,DC=com"
What I need is to drop the CN=username2(username2), part of the result. So that my output looks like:
"user2","user2 description","OU=OU1,OU=Site-2,OU=Accounts_User,DC=domain,DC=company,DC=com"
You can use Select-Object dynamic expressions to manipulate a property and return that as part of your results, by constructing a select expression like:
Select-Object #{Name="NewName"; Expression={..any scriptblock..}}
In your case, I would remove the unwanted string part with a regex, by matching the CN=([^=]*),OU= and leaving only the OU= part only:
$_.DistinguishedName -ireplace "CN=([^=]*),OU=","OU="
So Altogether:
get-content users.txt |
get-aduser -pr SamAccountName,Description,DistinguishedName |
Select SamAccountName,Description, #{Name="NewDN"; Expression={$_.DistinguishedName -ireplace "CN=([^=]*),OU=","OU="}} |
Export-Csv users.csv

How do I add a prefix to Format-List output?

I need to generate an import file in the format of sip:username#domain.com for the Lync User Management tool.
I'm using this PowerShell commmand:
Get-DistributionGroupmember FirmOperationS#company.com | Select-Object primarysmtpaddress
I'm having trouble prepending the characters "sip:" to the column output, and saving it to a CSV using Export-Csv.
Try a custom/calculated property:
Get-DistributionGroupmember FirmOperationS#company.com |
Select-Object #{n="PrimarySMTPAddress";e={ "sip:$($_.primarysmtpaddress)" }}
If you only need that property, you could also replace Select-Object with Foreach-Object, like
Get-DistributionGroupmember FirmOperationS#company.com |
ForEach-Object { "sip:$($_.primarysmtpaddress)" }

Powershell - Get-DistributionGroup exporting to csv issue

The below command will output the correct information to the screen, however when exporting to a csv file it looks nothing like what is shown on the screen. I have been able to export with other cmdlets, Get-DistributionGroup seems to corrupt the data when using with export.csv.
Outputting to screen works great!
Get-DistributionGroup | Where {$_.emailaddresses -like "*#adomain.com*"} | FT Name,Alias,EmailAddresses
Exporting to csv doesnt work correctly
Get-DistributionGroup | Where {$_.emailaddresses -like "*#adomain.com*"} | FT Name,Alias,EmailAddresses | Export-csv C:/thisis.csv
Instead of Format-Table (alias: ft) you should use Select-Object. Export-Csv expects objects as input, not formating instructions.
Format-Table, by definition, will convert objects to something that looks well in your output, but it's one way trip: you loose original objects as a part of the process.
Select-Object on the other hand can create objects with subset of properties, so that cmdlets like Export-Csv can still use data from original source.
EDIT
Thing I missed originally: you try to export a property that is a collection (EmailAddresses). That won't work unless you 'flatten' the collection first:
Get-DistributionGroup | Where {$_.emailaddresses -like "*#adomain.com*"} | select Name,Alias, #{
Name = 'EmailAddresses'
Expression = { $_.EmailAddresses -join '|' }
}
Thank you the answer has been resolved.
BartekB's answer
Get-DistributionGroup | Where {$.emailaddresses -like "*#adomain.com*"} | select Name,Alias, #{
Name = 'EmailAddresses'
Expression = { $.EmailAddresses -join '|' }
}