Merge CSV files with filtering - csv

I've started to play around with PowerShell some time ago, in order to filter some logs one of my servers is creating.
The individual log is a CSV in text file, where first line is some info about the process creating it. Headers are on the 2nd line, and the actual things are on the 3rd. There are about 15 properties, but I only need couple of them.
Here is what works for me flawlessly on one file:
Import-csv file.txt | Select-Object -Skip 1 -Property prop1, prop2, prop3, prop4, prop5 | Export-csv result.csv -NoTypeInformation
But, whatever I tried to use for multiple files (let's say, all .txt files in said folder, since the logs are created per day, and grouped in folders), it doesn't work for me, and I suspect it's because of the different first line, which I try to skip the same way, but I then get empty merged CSV file with only prop1 as 1st column
Any help is appreciated, thanks!

If the headers are actually on the second line, not the first, then you should probably do
Get-Content file.txt | Select-Object -Skip 1 | ConvertFrom-Csv | Export-Csv result.csv -NoTypeInformation
Because this strips the first line before it gets parsed as CSV.
If you want to merge multiple files in the same way, you can do that similarly:
Get-ChildItem *.txt | ForEach-Object {
Get-Content $_ | Select-Object -Skip 1 | ConvertFrom-Csv
} | Export-Csv result.csv -NoTypeInformation

Related

delete first row of multiple csv in a folder windows command line

I have multiple csv files in a folder.
I want to delete the first row of each csv in the folder using windows command line.
I am not familiar with windows command line so I will need information regarding how to call the folder within the console.
I do not want to make new files with the "subtracted" row, I just want to replace the original file or overwrite it.
Use Powershell
Get-ChildItem "path\to\your\directory" -Filter *.csv |
Foreach-Object {
$import = Get-Content $_.FullName
$import | Select-Object -Skip 1 | Set-Content $_.FullName
}

CSV:Powershell: Deleting rows containing specific word in the cell

I am having trouble in parsing the csv and deleting the rows as per the requirement.
My csv file format is not even, and there are many columns which are blank, But I want them to be considered as proper cells
csv has some rows (not fixed number of rows) before the actual header and at the bottom there may be a chance of having 2 lines. I want my csv to start from the header instead of the first line.
Import-Csv file.csv | Get-Member
this code only gives me two member types but csv has more than 20columns
csv example:
1,ab
x,33,455
blank line
x,12,134
some more lines
f,er,fsdvf
blank lines
headers start here, 20+columns
Thanks in advance
If you know the header (columns) you can always force Import-Csv to have these columns even if the first line of the file suggests otherwise:
Import-Csv file.csv -Header One, Two, Three, Four
Downside is that you end up with some objects that you probably don't need.
As alternative, you can find line with your header and use Get-Content/ConvertFrom-Csv combo:
$line = Select-String -Path file.csv -Pattern ^One |
ForEach-Object LineNumber
Get-Content -Path file.csv |
Select-Object -Skip ($line - 1) |
ConvertFrom-Csv
Regular expression should probably be more verbose, but overall that should be easiest that convincing application producing fake CSVs to the it proper.

Can I assign Variables from selected Row using out-gridview -passthru

I'm very new to PowerShell and I'm trying to build on older batch files that I made into PowerShell and add some features.
At the moment I have a CSV file which I've used in the pass as a sort of "environment" file, previously I would do batch jobs against this CSV file.
I have a line
Import-Csv "csvfile" | select-object -property * | out-gridview -passthru
The CSV file is built something like:
Name,location,folder
Test,e,Testsite
Test1,c,windows
test2,c,temp
Basically I want to select one of the grows and click Okay and assign the 3 items to variables.. $foldername,$driveLetter,$destinationDirectory
I've looked high and low and I can't seem to manage it I did find one example on StackOverflow which I shamelessly copied, massaged and got to work ... but that gridview is prebuilt by the OP of that post and doesn't have things like the piping to grid-view.-Passthru has (Filter & scroll bar) but I was able to assign variables using this method but my CSV is pretty huge and I want to be able to have it auto size itself and filter / scroll.
You need to use the -OutPutMode Single option of Out-Gridview to restrict selection to a single item from the gridview.
Import-Csv "csvfile" |
select-object -property * |
out-gridview -OutputMode Single -Title 'Select a row' |
ForEach-Object {
$foldername,$driveLetter,$destinationDirectory = $_.Name,$_.location,$_.folder
}

Refer to .CSV for filenames, then Locate and Delete Files Recursively

I'm trying to write a PowerShell script which refers to a CSV file for a list of banned file names (mostly games) and removes them from my users' home folders. I've got it working in the root or target directory, but cannot seem to make it recursive, so that it drills-down through subfolders, despite trying to utilise the -recurse parameter.
As you'll see, I'm not much of a coder, but am trying to learn and better myself. My PS script looks like this:
cd "C:\Test user"
Import-Csv "C:\Games.csv" | foreach {Remove-Item $_.Game -Verbose -Recurse}
and my CSV file looks like this:
Game,Game1.swf,Game2.swf,Game3.swf
Any advice as to what I am missing in order to make this work recursively would be hugely appreciated. Thank you all in advance for being so generous with your time.
You're misunderstanding the structure of CSVs. Essentially CSV is a way to store tabular data (data organized in rows and columns). Your file would have to have the following structure if you want it to work with the code you posted:
Game
Game1.swf
Game2.swf
Game3.swf
The data you posted is just a comma-separated list of values. To process this string you need something like this:
(Get-Content "C:\Games.csv") -split ',' | Remove-Item -Verbose -Recurse
or perhaps like this (if you want to skip the first element):
(Get-Content "C:\Games.csv") -split ',' |
Select-Object -Skip 1 |
Remove-Item -Verbose -Recurse
Edit: If you need to recursively search a folder tree for files from your CSV and then delete just the files you'd do it like this:
$root = 'C:\root\folder'
$items = (Get-Content "C:\Games.csv") -split ',' | Select-Object -Skip 1
Get-ChildItem $root -Recurse -Force |
Where-Object { $items -contains $_.Name } |
Remove-Item -Verbose

how to grab specific json nested values in powershell?

I have this command here:
(Get-Content output.json -Raw | ConvertFrom-Json) | Convertto-CSV -NoTypeInformation
that pulls json file and puts all the top levels into a csv format. however, i want some nested values inside there as well. I know there is a command -depth that wlil pull all values at a certain depth, but I only wnat to specify a certain one. For example, if I want to pull /data/1/structure/name, how would I get that value specifically as well?
Use Select-object:
(Get-Content output.json -Raw |Select value1,Valu2,Valu | ConvertFrom-Json) | Convertto-CSV -NoTypeInformation