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

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

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
}

Moving files based on CSV values

noobie here.
I have a csv file with two columns, they specify source files paths and destinated paths. I have around 1500+ lines to execute. Is there any way to batch process this via a bat file or anything else?
A line in my CSV looks like this:
Source Path,Dest Path
C:\Users\Nick\Pictures\XXXXXXX.img,C:\Users\Nick\Pictures\Export\XXXXXXX.img
I'd probably not solve this with batch files, but rather with PowerShell. It's possible in batch, but notoriously unreliable, especially around characters you don't expect when starting out.
In PowerShell this could be as simple as
Import-Csv files.csv | ForEach-Object {
Move-Item -LiteralPath $_.'Source Path' -Destination $_.'Dest Path'
}
You might need to create directories as required, perhaps something like this:
Import-Csv files.csv | ForEach-Object {
New-Item -ItemType Directory (Split-Path -Parent $_.'Source Path')
Move-Item -LiteralPath $_.'Source Path' -Destination $_.'Dest Path'
}

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
}

Merge CSV files with filtering

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

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