Powershell .csv foreach row add same value - powershell

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

Related

Combining output to CSV from two Powershell commands

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

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"

PowerShell: Import CSV File, Count Group Sizes, And Output To Original File Appending New Column With Counts

I have a test input csv file, as follows:
ID;Name;Level
1;Alpha;A
2;Bravo;A
3;Charlie;A
4;Delta;A
5;Echo;A
6;Foxtrot;A
7;Golf;A
1;Hotel;B
2;India;B
3;Juliet;B
1;Kilo;C
2;Lima;C
3;Mike;C
4;November;C
5;Oscar;C
and I would like to generate the following output file:
ID;Name;Level;Number
1;Alpha;A;7
2;Bravo;A;7
3;Charlie;A;7
4;Delta;A;7
5;Echo;A;7
6;Foxtrot;A;7
7;Golf;A;7
1;Hotel;B;3
2;India;B;3
3;Juliet;B;3
1;Kilo;C;5
2;Lima;C;5
3;Mike;C;5
4;November;C;5
5;Oscar;C;5
I have the following code snippet which, though it calculates the correct group counts, does not output anything to csv:
Import-Csv '.\TestInput.csv' -Delimiter ';' |
Group-Object 'Level' |
Select-Object -Expand Count |
Export-Csv -NoTypeInformation '.\TestOutput.csv'
What am I missing?
Break the Number count out, give this a try
$data = Import-Csv '.\TestInput.csv' -Delimiter ';'
$levelCount = $data | Group-Object -Property 'Level' -AsHashTable
$data | Add-Member -MemberType ScriptProperty -Name Number -Value {$levelCount[$this.Level].count} -PassThru | Export-Csv -NoTypeInformation '.\TestOutput.csv'

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 |
...

How to write the header from a .csv file to an array using powershell?

How do I read only the head from a CSV file and write the columnn names into an array?
I have found a solution using following cmdlets:
$obj = Import-Csv '.\users.csv' -Delimiter ';'
$headerarray = ($obj | Get-member -MemberType 'NoteProperty' | Select-Object -ExpandProperty 'Name')
But the problem is the name - values are auto sorted alphabetic
Anyone has a solution for this?
You can get the column names of a CSV file like this:
import-csv <csvfilename> |
select-object -first 1 | foreach-object { $_.PSObject.Properties } |
select-object -expandproperty Name