Combining output to CSV from two Powershell commands - powershell

I am trying to take an existing CSV file and append it with the output from another command. Whenever I run the script, it sets all values in the column to the $owner.
I'm not sure what I'm missing, but it is not working properly.
`Connect-MicrosoftTeams
#get list of teams
$TeamsFile = Get-Team | Select DisplayName, GroupID, Description | Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation
#read row in CSV, add owner to new column.
ForEach ($Team in $TeamsFile) {
$Owner = Get-TeamUser -GroupId $Team.GroupID -Role Owner | Select Name
Write-Host $Team.GroupID " Owner: " $Owner.name
$TeamsFile = Import-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv"
$TeamsFile | Select-Object -Property *, #{label = 'Team Owner'; expression = {$Owner.name}}
}
$TeamsFile | Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation
`
Sample of existint CSV

If you only need a merged output of two commands, you may make use of calculated properties:
Get-Team |
Select DisplayName,GroupID,Description,
#{n='Team Owner';e={(Get-TeamUser -GroupId $_.GroupID -Role Owner).Name}}
If you already have a CSV file of Team data and need just the owner, you may update your CSV row objects with a new property using a calculated property also:
(Import-CSV -LiteralPath 'C:\Scripts\Microsoft_Teams_List.csv' |
Select *,#{n='Team Owner';e={(Get-TeamUser -GroupId $_.GroupID -Role Owner).Name}}) |
Export-Csv -LiteralPath 'C:\Scripts\Microsoft_Teams_List.csv' -NoTypeInformation
In your attempt, the following code does not yield the results you think:
$TeamsFile = Get-Team | Select DisplayName, GroupID, Description | Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation
$TeamsFile won't contain any output because all output was sent down the pipeline to Export-Csv. If you want to capture output before it is sent to Export-Csv, you can use the common parameter -OutVariable.
Get-Team | Select DisplayName, GroupID, Description -OutVariable TeamsFile |
Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation
$TeamsFile # now contains your Teams

If headers are the same, then you can keep source csv files in a directory and use this command to generate a combined csv.
Get-ChildItem -Filter *.csv | Select-Object -ExpandProperty FullName | Import-Csv | Export-Csv .\combinedreport.csv -NoTypeInformation

Related

export csv rows where duplicate values found in column

I have a csv file where I am trying to export rows into another csv file only where the values in the id column have duplicates.
I have the following csv file...
"id","blablah"
"valOne","valTwo"
"valOne","asdfdsa"
"valThree","valFour"
"valFive","valSix"
"valFive","qwreweq"
"valSeven","valEight"
I need the output csv file to look like the following...
"valOne","valTwo"
"valOne","asdfdsa"
"valFive","valSix"
"valFive","qwreweq"
Here is the code I have so far:
$inputCsv = Import-CSV './test.csv' -delimiter ","
#$output = #()
$inputCsv | Group-Object -prop id, blablah | Where-Object {$_.id -gt 1} |
Select-Object
##{n='id';e={$_.Group[0].id}},
##{n='blablah';e={$_.Group[0].blablah}}
#Export-Csv 'C:\scripts\powershell\output.csv' -NoTypeInformation
#Write-Host $output
#$output | Export-Csv 'C:\scripts\powershell\output.csv' -NoTypeInformation
I've searched multiple how-to's but can't seem to find the write syntax. Can anyone help with this?
Just group on the ID property and if there is more than 1 count in the group then expand those and export.
$inputCsv = Import-CSV './test.csv' -delimiter ","
$inputCsv |
Group-Object -Property ID |
Where-Object count -gt 1 |
Select-Object -ExpandProperty group |
Export-Csv output.csv -NoTypeInformation
output.csv will contain
"id","blablah"
"valOne","valTwo"
"valOne","asdfdsa"
"valFive","valSix"
"valFive","qwreweq"

Getting only a repeating files from directory and subdirectories

I'm trying to do script for finding non-unique files.
The script should take one .csv file with data: name of files, LastWriteTime and Length. Then I try to make another .csv based on that one, which will contain only those objects whose combination of Name+Length+LastWriteTime is NON-unique.
I tried following script which uses $csvfile containing files list:
$csvdata = Import-Csv -Path $csvfile -Delimiter '|'
$csvdata |
Group-Object -Property Name, LastWriteTime, Length |
Where-Object -FilterScript { $_.Count -gt 1 } |
Select-Object -ExpandProperty Group -Unique |
Export-Csv $csvfile2 -Delimiter '|' -NoTypeInformation -Encoding Unicode
$csvfile was created by:
{
Get-ChildItem -Path $mainFolderPath -Recurse -File |
Sort-Object $sortMode |
Select-Object Name, LastWriteTime, Length, Directory |
Export-Csv $csvfile -Delimiter '|' -NoTypeInformation -Encoding Unicode
}
(Get-Content $csvfile) |
ForEach-Object { $_ -replace '"' } |
Out-File $csvfile -Encoding Unicode
But somehow in another $csvfile2 there is only the one (first) non-unique record. Does anyone have an idea how to improve it so it can list all non-unique records?
You need to use -Property * -Unique to get a list of unique objects. However, you cannot use -Property and -ExpandProperty at the same time here, because you want the latter parameter to apply to the input objects ($_) and the former parameter to apply to an already expanded property of those input objects ($_.Group).
Expand the property Group first, then select the unique objects:
... |
Select-Object -ExpandProperty Group |
Select-Object -Property * -Unique |
...

Powershell .csv foreach row add same value

I am exporting information about members of group from AD and I want to pass them a 'target' value in new column.
I was able to find out how to create a new header, but not how to add values. It should be rather simple, because right now I just need to pass the same value for all users.
Import-Csv unformatted.csv | Select-Object *,"Target" | Export-csv Target.csv -NoTypeInformation
LogonName,Email,Target
abc,abc#a.com,
bcd,bcd#a.com,
cde,cde#a.com,
I want to see such result
LogonName,Email,Target
abc,abc#a.com,TARGET
bcd,bcd#a.com,TARGET
cde,cde#a.com,TARGET
You are very close indeed. Use Calculated properties like this -
Import-Csv unformatted.csv | Select-Object *,#{Name='Target';Expression={'TARGET'}} | Export-csv Target.csv -NoTypeInformation
OR
You can add property to the objects like this -
$csv = import-csv -path unformatted.csv -header LogonName,Email
foreach($item in $csv)
{
Add-Member -Input $item -MemberType NoteProperty -Name Target -Value 'TARGET'
}
$csv | Select-Object LogonName, Email, Target | export-csv -path Target.csv -NoTypeInformation
Try this:
Import-Csv unformatted.csv | Select-Object *, #{name="Target"; expression={"TARGRET"}} | Export-csv Target.csv -NoTypeInformation

find dot in CSV and replace with found names in Active Directory group

I have a TXT file and converted it to CSV.
Now I want to have a script which:
Look in UserID column and look if there is a name with dot in it (ex: xyz.aaa). The names with dot in them are not UserID but they are Group.
Then it should look for them in AD.
Then replace the found names in the same CSV file.
Find all CNs in TXT file and list them in CSV File:
Select-String -Path $TXTFile -Pattern 'CN=(.*?),' -AllMatches |
Select-Object -Expand Matches |
ForEach-Object { $_.Groups[1].Value } |
select #{L="UserID"; E={$_}} |
Export-CSV $CSVFile1 -Delimiter ";"
what I tried and it does not work, which definately wrong is:
look for . in UserID column which are Groups, then look for them in AD. Replace the members in Groupnames.
Get-ChildItem -Path $CSVFile1 | ForEach-Object {
(Get-Content $_.UserID).Replace('.','Get-ADGroupMember -identity "$_.UserID" -Recursive | Select Name') | Out-File $_.UserID
}
Something like this should do the trick I think
$ProcessedList = Import-Csv -Path $CSVFile1 -Delimiter ';' |
Select-Object -Property *,
#{
Name='ProcessedUserID';
Expression={
if ($_.UserID -match '\.') {
(Get-ADGroupMember -Identity $_.UserID |
Select-Object -ExpandProperty SamAccountName) -join ','
}
else {
$_.UserID
}
}
}
$ProcessedList
$ProcessedList | Export-CSV -Path 'Whatever' -NoTypeInformation -Delimiter ';'

Possible to combine .csv where-object filters?

I'm trying to filter a .csv file based on a location column. The column has various location entries and I only need information from the rows that contain certain locations, that information then gets exported out to a separate .csv file. I can get it to work by searching the .csv file multiple times with each location filter, but I haven't had any luck when trying to combine it into 1 search.
What I have now is:
$csv = Import-Csv "${filepath}\temp1.csv"
$csv | Where-Object location -like "co*" | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -NoTypeInformation
$csv | Where-Object location -like "cc*" | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -Append -NoTypeInformation
$csv | Where-Object location -like "dc*" | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -Append -NoTypeInformation
$csv | Where-Object location -like "mf*" | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -Append -NoTypeInformation
What I'd like to have is something like below. I don't get any errors with it, but all I get is a blank .csv file:
$locations = "co*","cc*","dc*","mf*"
$csv = Import-Csv "${filepath}\temp1.csv"
$csv | Where-Object location -like $locations | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -NoTypeInformation
I've been lurking here for a while and I'm usually able to frankenstein a script together from what I find, but I can't seem to find anything on this. Thanks for your help.
You can replace multiple -like tests with a single -match test using an alternating regex:
$csv = Import-Csv "${filepath}\temp1.csv"
$csv | Where-Object {$_.location -match '^(co|cc|dc|mf)'} |
select EmployeeNumber |
Export-Csv "${filepath}\disablelist.csv" -NoTypeInformation
You can build that regex from a string array:
$locations = 'co','cc','dc','mf'
$LocationMatch = '^({0})' -f ($locations -join '|')
$csv = Import-Csv "${filepath}\temp1.csv"
$csv | Where-Object { $_.location -match $LocationMatch } |
select EmployeeNumber |
Export-Csv "${filepath}\disablelist.csv" -NoTypeInformation