I have a CSV file with 3 columns: Name, Job, And Directory.
I'm trying to Have the user type in Either a Name, or a Job, and it will return one or multiple lines output from column 3 (Directories)
$Profile = $PSScriptRoot "\Profile.csv"
$Data = Import-Csv -Path $Profile
$JobChoice = Read-Host "Enter a Backup option"
$Results = $Data | Where-Object {( $_.Name, $_.Job ) -eq [string]$JobChoice}
$Results | Select-Object -ExpandProperty ($_.Directory)
The above example if I Type when prompted: "satisfactory" it will only show one return. but it shows all three columns i just need it to return the current lines directory.
Also, If I enter "SaveGame" as the Read-Host input. it will return all games with that job, but only the directories. I Would like to keep this feature as it accepts one game or multiple based on if i choose Name, or Job
I have even tried every combination of:
Select-Object -ExpandProperty ($_.Directory)
My goal of this is to add the data to the CSV then parse it and send them to xcopy to batch copy the returned directories.
Any help?
The CSV:
Name,Job,Directory
Satisfactory,Savegame,C:\Users\jrkid\AppData\Local\FactoryGame\Saved\SaveGames
TheLongDark,Savegame,C:\Users\jrkid\AppData\Local\Hinterland
TheInfected,Savegame,C:\Users\jrkid\AppData\Local\TheInfected
WrenchGame,Savegame,C:\Users\jrkid\AppData\Local\WrenchGame
Valheim,Savegame,C:\Users\jrkid\AppData\LocalLow\IronGate
RisingWorld,Savegame,C:\Users\jrkid\AppData\LocalLow\JIW-Games
7DTD,Savegame,C:\Users\jrkid\AppData\Roaming\7DaysToDie
CodePudding user response:
You can use the following statement, which can be read as:
An object of $csv
where the value of the Property Name
OR the value of the property Job
are equal to $JobChoice
. Then you can follow that statement with .Directory
to enumerate all values of the Directory
property.
See Member Enumerations for details.
$csv = Import-Csv path/to/csv.csv
$JobChoice = Read-Host "Enter a Backup option"
$csv.Where({ $JobChoice -eq $_.Name -or $JobChoice -eq $_.Job }).Directory
Looking back at your code, the statement you're currently using would also work too, however -in
or -contains
would be more efficient.
$csv.Where({ ($_.Name, $_.Job) -contains $JobChoice }).Directory
The only issue with your code was the use of ($_.Directory)
, it should have been just:
| Select-Object -ExpandProperty Directory
CodePudding user response:
Are you just wanting the Directory as the output? Replace Select-Object with ForEach-Object and use {} instead of ()
$Results | ForEach-Object { $_.Directory}