1

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
Justin.k
  • 9
  • 3

3 Answers3

1

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
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
0

Are you just wanting the Directory as the output? Replace Select-Object with ForEach-Object and use {} instead of ()

$Results | ForEach-Object { $_.Directory}
Darin
  • 1,423
  • 1
  • 10
  • 12
0

This Code Was the solution i needed.

$csv.Where({ ($_.Name, $_.Job) -contains $JobChoice }).Directory

This allows me to extract the pipeline directory from the CSV File. Below is the working script.

CSV

Name           Type     DirType    Directory                    
7DTD           Game      User     Roaming\7DaysToDie                    
ASXP        Application  User     Roaming\HiFi                  
BattleNet   Application  User     Local\Battle.net                  
CereProc    Application  User     Local\CereProc

                

Variables

$global:Profile = $PSScriptRoot + "\Profile.csv"
$global:Data = Import-Csv -Path $global:Profile
$global:BackupPath = "E:\PowerShell Scripts\Automate Backup\MainFrameV2\Test1"
$global:LogFile = $PSScriptRoot + "\Log.txt"
$global:AppData = $env:USERPROFILE+"\AppData"
$global:ProgramFiles = $env:ProgramFiles
$global:ProgramFiles86 = $env:ProgramFiles+" (x86)"
$global:ProgramData = $env:ProgramData
$global:DateFormat = "(%m-%d-%Y)"
$global:cWaitTime = 2 # Retry copy wait time

$global:TypeChoice = Read-Host

Robocopy With the Working Code

$global:Data | Where({ ($global:TypeChoice -eq $_.Type -or $global:TypeChoice -eq $_.Name) -and $_.DirType -eq "USER"}) | ForEach  {                   
                            robocopy ($global:AppData+"\"+$_.Directory) ($global:BackupPath +"\"+$global:Time+"\"+($_.Type)+"\"+ ($_.Name))  /S /mt:8 /tee /eta /log+:$global:LogFile /xf "*.ccc" /w:$global:cWaitTime
                            trap { exit 8 }
Justin.k
  • 9
  • 3