I am attempting to create a menu for restoring backups. Ideally, the user will see a list of folders, each numbered and then be prompted to choose. The list and selection options will need to be dynamic.
The problem is my limited understanding of loops and arrays. Here is the array I am trying to work with:
$saved_backups = Get-ChildItem $backup_folder |Where-Object {$_.Name -like "*$target_username"} | select-object @{n="Saved Backups:";e={$_.Name}}
[array]::Reverse($saved_backups)
From the user's perspective, I would like it to look like:
1) $saved_backups[0]
2) $saved_backups[1]
or
1) 2015.04.21_laptop
2) 2015.04.20_laptop
Where backup is actually a folder name. The user would then select the number and the selection saved to a variable I can use later.
Any help is appreciated!
EDIT #1: I was able to simplify the folder name needed with the following:
$saved_backups = Get-ChildItem $backup_folder |Where-Object {$_.Name -like "*$target_username"}
[array]::Reverse($saved_backups)
$saved_backups = $saved_backups.name
EDIT #2: Thanks to Matt, I am starting to understand the idea of using the index. The problem now is with my array, as it is counting both objects as one, then counting it twice.
$saved_backups = Get-ChildItem $backup_folder |Where-Object {$_.Name -like "*$target_username"}
[array]::Reverse($saved_backups)
$saved_backups = $saved_backups.name
For($folderIndex = 1; $folderIndex -le $saved_backups.Count; $folderIndex++){
Write-Host "[$folderIndex]$saved_backups"
}
Current Output:
[1] Folder1 Folder2
[2] Folder1 Folder2
Ideally it would look like:
[1] Folder1
[2] Folder2
Once I understand the for statement, I will work on the selection menu.
