How can I create each folder only once while processing a CSV in PowerShell?

0
1
Asked By VelvetMango42 On

I need to regularly migrate shortcuts using PowerShell. An API export gives me CSV rows containing a user ID, folder name, parent folder ID, target ID, and shortcut name. For the special value, the shortcut should be created directly in the root. For any other folder, I need to create the folder through one API POST, capture the new folder ID from the response, and then create all shortcuts belonging to that folder using the captured ID.

The CSV can contain several shortcuts in the same folder, for example multiple rows for Archive or Personal. My current loop processes every CSV row, so putting the folder-creation request directly inside it would create duplicate folders. What is the best PowerShell workflow for creating each required folder once, remembering its returned ID, and then using that ID when creating the related shortcuts?

3 Answers

Answered By SilverOtter31 On

The existing code also imports the CSV twice and appears to use inconsistent column names. Import it once and use the actual headers consistently. `Select-Object -ExpandProperty Folder -Unique` is fine for a simple list, but it is not enough if the same folder name can occur under different users or parents.

You can also keep a list of created folder names, but a hashtable is better because you need to retrieve the corresponding API ID. Avoid repeatedly using `+=` for large collections; hashtables or generic lists are more efficient. Whether the API rejects duplicates or creates multiple folders is an API behavior, so the script should enforce uniqueness itself rather than rely on the endpoint.

Answered By BrightPine6 On

Another clear approach is to use two phases. First group the non-root rows by their folder identity, create one folder for each group, and store the returned IDs. Then loop through the original rows and create the shortcuts.

```powershell
$rows = Import-Csv $infile
$folderIds = @{}

$groups = $rows | Where-Object FOLDERNAME -ne '' |
Group-Object USERID, PARENTID, FOLDERNAME

foreach ($group in $groups) {
$first = $group.Group[0]
$response = New-RemoteFolder -Name $first.FOLDERNAME -ParentId $first.PARENTID
$key = '{0}|{1}|{2}' -f $first.USERID, $first.PARENTID, $first.FOLDERNAME
$folderIds[$key] = $response.id
}

foreach ($row in $rows) {
if ($row.FOLDERNAME -eq '') {
$destinationId = $row.PARENTID
}
else {
$key = '{0}|{1}|{2}' -f $row.USERID, $row.PARENTID, $row.FOLDERNAME
$destinationId = $folderIds[$key]
}

New-Shortcut -ParentId $destinationId -Name $row.SHORTCUTNAME -TargetId $row.TARGETID
}
```

This separates folder creation from shortcut creation and makes it impossible for the shortcut loop to create the same folder repeatedly.

Answered By QuietHarbor7 On

You do not need a double loop over every distinct folder and every CSV row. Import the CSV once, then keep a cache of folders that have already been created. A hashtable works well because it maps a folder key to the API-generated folder ID.

The key should include every value that defines a unique folder. If folders are scoped to a user or parent, use something like "$($row.USERID)|$($row.PARENTID)|$($row.FOLDERNAME)" rather than only the folder name.

```powershell
$rows = Import-Csv -Path $infile
$folderIds = @{}

foreach ($row in $rows) {
if ($row.FOLDERNAME -eq '') {
$destinationId = $row.PARENTID
}
else {
$key = '{0}|{1}|{2}' -f $row.USERID, $row.PARENTID, $row.FOLDERNAME

if (-not $folderIds.ContainsKey($key)) {
$response = New-RemoteFolder -Name $row.FOLDERNAME -ParentId $row.PARENTID
$folderIds[$key] = $response.id
}

$destinationId = $folderIds[$key]
}

New-Shortcut -ParentId $destinationId -Name $row.SHORTCUTNAME -TargetId $row.TARGETID
}
```

The first row for a folder creates it and stores the returned ID. Later rows find that ID in the hashtable and skip the folder POST.

CopperLynx19 -

Using only the folder name as the key could incorrectly combine folders belonging to different users or parent folders. Include the user and parent identifiers whenever those affect where the folder lives.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.