I'm using the following to import users from a csv file and output them to the console:
$(Import-csv $SelectedFile | Select -ExpandProperty EmailAddress)
This displays each EmailAddress on a separate line in the console but I want to output this to a log file too.
Using the following will do this but not a separate line for each EmailAddress like the above. Instead it just shows them on one line with no separator:
"$(Import-csv $SelectedFile | Select -ExpandProperty EmailAddress)" | Tee-Object $UserMigrationLog -Append
Is it possible to do what I ask within this one command?
You interpolate the result of Import-CSV into a single string. Just remove the subexpression "$()" and it will work:
Import-csv $SelectedFile | Select -ExpandProperty EmailAddress | Tee-Object $UserMigrationLog -Append
Related
I'm trying to get a list of AD Group owners from a CSV file, the second column is what I want to pull from, it is titled Owners I want to keep the output if there are failures as well. Would I just modify the "foreach" portion of my code?
Get-Content "C:\tmp\SMB.csv" -OutVariable | foreach {
Get-ADGroup -identity $_ -properties name,managedby| select name,managedby |
out-file c:\temp\test.csv -Append }
To get the owners values just use
Get-Content 'C:\tmp\SMB.csv' | ConvertFrom-Csv | Select Owners
I'm trying to split a csv file by the first digits of the longitude column. Here is a sample:
X,Y,TYPE,SPEED,DirType,Direction
-44.058251,-19.945982,1,30,1,339
-54.629503,-20.497509,1,30,1,263
-54.646202,-20.496151,1,30,1,86
I have no powershell knowledge but I found some script online and it did what I wanted:
Import-Csv maparadar.csv
| Group-Object -Property {($_.x)[0..2] -join ""}
| Foreach-Object {$path=$_.name+".csv" ; $_.group
| Export-Csv -Path $path -NoTypeInformation}
With this I get output files like -44.csv, -54.csv
But it adds unwanted quotes to every field in the output file like:
"X","Y","TYPE","SPEED","DirType","Direction"
"-46.521991","-23.690235","1","30","1","169"
"-46.670774","-23.756021","1","30","1","281"
"-46.549897","-23.120720","1","30","1","99"
Is there any way I can export the csv without adding those quotes?
The following should provide the desired output:
Import-Csv maparadar.csv |
Group-Object -Property {($_.x)[0..2] -join ""} |
Foreach-Object { $path=$_.name+".csv" ; ($_.group |
ConvertTo-Csv -NoTypeInformation) -Replace '"' |
Set-Content -Path $path }
Explanation:
We replaced your Export-Csv with ConvertTo-Csv, which provides the CSV output to the console/pipeline rather than outputting to the file. Those CSV formatted outputs are sent through the -Replace operator to replace the literal " characters. Finally the formatted output is sent to the desired file using Set-Content -Path $path.
I am new to powershell ... I am trying to filter one of my scan result (.csv) on specific value, I am able to do this for simple CSV files. however the automated scan result seems like nested.
I need to filter Column "F" where the value is "Vuln" (Image is added) and write to a new CSV file.
can anyone please give me some leads.
I tried simple lines like : (but I didnt get any result)
Import-CSV -Path "C:\test.csv" | Where-Object {$_.Type -ey"Vuln"}
Sample CSV file format
You're mostly there, it looks like problem is coming from that .CSV file.
Frankly, it isn't a truly valid csv file, since it isn't just a simple Comma Separated Value sheet, instead it has multiple types of data.
However, it looks like something close to a real .CSV begins on line 6, so what we'll need to do is skip the first few rows of the file and then try to convert from .csv. I made my own .csv like yours
I can read the file and skip the first five lines like so:
Get-Content C:\temp\input.csv | Select-Object -Skip 5 | ConvertFrom-Csv
HostName OS Type
-------- -- ----
SomePC123 WinXp Vuln
SomePC234 Win7 Vuln
SomePc345 Win10 Patched
And then filter down to just items of Type Vuln with this command:
Get-Content C:\temp\input.csv | Select-Object -Skip 5 | ConvertFrom-Csv | Where-Object Type -eq 'Vuln'
HostName OS Type
-------- -- ----
SomePC123 WinXp Vuln
SomePC234 Win7 Vuln
To use this, just count down the number of lines until the spreadsheet begins within your .CSV and edit the -Skip parameter to match.
If you want to keep the header information, you can use an approach like this one:
$crap = Get-Content C:\temp\input.csv | Select-Object -First 5
$OnlyVulns = Get-Content C:\temp\input.csv | Select-Object -Skip 5 | ConvertFrom-Csv | Where-Object Type -eq 'Vuln'
$CrapAndOnlyVulns = $crap + $OnlyVulns
$CrapAndOnlyVulns > C:\pathTo\NewFile.csv
Here is the final script :)
$import = get-content .\Scan.csv
$import1= $import | Select-Object -First 7
$import | Select-Object -Skip 7 | ConvertFrom-Csv | Where-Object {$_.Type -eq "Vuln"} | Export-Csv Output1.csv -NoClobber -NoTypeInformation
$import1 + (Get-Content Output1.csv) | Set-Content Output2.csv
Is there a way to include text qualifiers i.e. "Column 1"|"Column 2" when using PowerShell to export to a csv file?
In the script below I remove the text qualifiers that were already transferred in the file because the first column didn't have any for some odd reason.
Get-MsolUser -all |
Where-Object {($_.IsLicensed -eq "True") -and ($_.Title -notlike "Volunteer") } |
Select-Object Firstname, LastName, Department, Title |
ConvertTo-Csv -NoTypeInformation -Delimiter "|"|
% { $_ -replace '","', "|"} | % { $_ -replace '"', ""} | out-file "$path\$file_name" -fo -en ascii
I think the output may be as you expect but you may be viewing the output file in Excel rather than with a text editor (I used Notepad++). When I tried to reproduce this I used the following code and viewed the output file in a text editor and each column and row of the output has text qualifiers as expected. However, when viewing the same file in Excel the first column is displayed by Excel as if it does not have text qualifiers. Here's the code I ran to test:
"[a known UPN from my tenant]","[a known UPN from my tenant]"|
foreach-object {Get-MsolUser -UserPrincipalName $_} |
Where-Object {($_.IsLicensed -eq "True") -and ($_.Title -notlike "Volunteer")} |
Select-Object Firstname, LastName, Department, Title |
ConvertTo-Csv -NoTypeInformation -Delimiter "|" |
out-file $path\$file_name" -fo -en ascii
The issue with text qualifiers missing in the first column first occurred 'before' I specified the -delimiter parameter, my code above is a bit of a fix and mashup of two methods of fixing that issue, and a bit redundant.
I'm able to generate the intended results with the script below:
$MyData = import-csv \\TestPath\licenses_v2.csv -delimiter '|' |
select-object FirstName,LastName,Department,Title,#{expression={'Office 365 AD'}};
Write-DbaDataTable -sqlinstance $server -database $database -table $table -inputobject $MyData -KeepNulls;
I am trying to append a CSV file. Here are the lines I am using. I wasn't able to find an append option for export-csv unfortunately. Any ideas would be helpful to get this to work.
Get-ADGroupMember "Domain Admins" | select name, samaccountname | Export-Csv c:\bin\DomainAdmins.csv
$admins = Import-Csv C:\bin\DomainAdmins.csv
foreach ($i in $admins) {Get-ADUser $i.samaccountname -properties * | select name, lastlogondate | Export-Csv c:\bin\dalogon.csv}
The documentation suggests that there is an -append flag. The example given ends with
| export-csv –append –path \\Archive01\Scripts\Scripts.csv
Have you tried that? It works fine for me. I'm on version 3, if that matters.
-Append was introduced with PowerShell v3, it's not available in PowerShell v2 and earlier. You can work around it like this, though:
... |
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
Out-File -Append "c:\bin\dalogon.csv"
I ran into this issue also a few days ago. There are a really two solutions I know in Powershell 2. The first would be to save all the data in an array and then use the export-csv commandlet. That did not work for me unless I rewrote my script. I needed to create a CSV and append line by line to build the file. So, I solved it with out-file -append and changing the encoding to ascii.
I basically created a string with my data in it and then piped it to out-file. Here is an example:
$myCSV = "C:\_PSScripts\data\myCSV.csv"
$firstOutputLine = "Column-1,Column-3,Column-3"
$firstOutputLine | out-file $myCSV -Encoding ascii -Force -Append
It's good! but it's only 1 column in CSV file! how to put to many columns as the code below:
Import-Module -Name 'C:\Program Files\Quest Software\Management Shell for AD\Quest.ActiveRoles.ArsPowerShellSnapIn.dll'
$group="HO-Internet","Internet1","Internet2","Internet3"
$group |ForEach-Object {Echo "--------------------Group Name $_ ----------------"; Get-QADGroupMember $_ | Select-Object Email,LogonName,ParentContainer,LastLogon,AccountIsDisabled |ConvertTo-Csv -NoTypeInformation `
| select -Skip 1 `
| Out-File -Append "D:\test.csv"}