TL;DR • It’s fine that both XQD cards individually mount as D:. • Differentiate them by VolumeLabel, not by drive letter. • Modify your script so the SMB share name = VolumeLabel (e.g. XQD_Card1, XQD_Card2). • Register your task once via PowerShell—after that, “hot-plug” automatically shares whichever card you inserted under the correct name.
Feel free to reach out if you want help auto-removing the share on eject, adding logging to see exactly when each card fires, or any other fine-tuning!
Explanation of Key Changes
- $labelPattern = "^XQD_Card" • This regular‐expression filter matches any volume whose label starts with XQD_Card. • If in the future you have XQD_Card3 or XQD_Card4, the same script will automatically match them.
- $thisShareName = $vol.FileSystemLabel • Instead of hard‐coding “XQDShare-D,” we use the actual label (XQD_Card1 or XQD_Card2) as the SMB share name. • That guarantees each card produces a different share name.
- Drive letter no longer matters • Because $vol.DriveLetter is just a temporary pointer (D:) until the card is ejected. • The script does not try to assign a specific drive letter; it just grabs whatever letter Windows gave it.
🔄 4) Re‐Register the Scheduled Task (One‐Time Step)
You already have a Scheduled Task named AutoShareXQD that watches for EventID 20001 (a new removable volume). Now that your script’s logic changed, you just need to confirm the task still points at the same path (C:\Scripts\my_xqd_smb_share.ps1). If you’ve not yet registered—or if you want to overwrite the old task—run this (as Administrator) in PowerShell:
# ─────────────────────────────────────────────────────────────────────────────
# 1) Remove any existing AutoShareXQD task so we can replace it cleanly
Unregister-ScheduledTask `
-TaskName "AutoShareXQD" `
-Confirm:$false `
-ErrorAction SilentlyContinue
# 2) Define the action (run our updated script)
$taskName = "AutoShareXQD"
$scriptPath = "C:\Scripts\my_xqd_smb_share.ps1"
$action = New-ScheduledTaskAction `
-Execute "powershell.exe" `
-Argument "-ExecutionPolicy Bypass -File `"$scriptPath`""
# 3) WMI XML filter: listen for Kernel-PnP EventID=20001 (removable volume mounted)
$xmlFilter = @"
<QueryList>
<Query Id="0" Path="System">
<Select Path="System">
*[System[Provider[@Name='Kernel-PnP'] and (EventID=20001)]]
</Select>
</Query>
</QueryList>
"@
$trigger = New-ScheduledTaskTrigger -OnEvent -Subscription $xmlFilter
# 4) Register under SYSTEM so it fires even if you’re logged out
Register-ScheduledTask `
-TaskName $taskName `
-Action $action `
-Trigger $trigger `
-RunLevel Highest `
-User "SYSTEM"
• Once you press Enter, Windows will create/update the AutoShareXQD task. • From now on, anytime you insert an XQD card whose label begins with XQD_Card…, Windows will: 1. Emit a Kernel‐PnP Event 20001 2. TaskScheduler sees that event → runs my_xqd_smb_share.ps1 3. Your script finds the volume by label (XQD_Card1 or XQD_Card2) and creates an SMB share named after that label.
📂 5) How to Access Each Card Over the Network • Card #1 (label = XQD_Card1) → share name = XQD_Card1 → network path =
\\<YourWindowsHostname>\XQD_Card1
• Card #2 (label = XQD_Card2) → share name = XQD_Card2 → network path =
\\<YourWindowsHostname>\XQD_Card2
Because each share is named exactly as the volume label, there is never a collision—even though both cards individually appear as D:\ on your desktop.
✅ 6) Is It “Good” That Both Always Use D:?
Yes, it’s perfectly fine. You only need to worry about drive letters if you: 1. Need to refer to a card’s exact file path in another script. 2. Want to assign a persistent letter to a given card, even across reboots/multiple insertions.
In your case, you don’t need to force a static letter (like always E:\ for Card #1 and F:\ for Card #2). Windows will pick the next free letter (D:\ if nothing else is mounted) every time. Your script no longer cares about which letter it is; it only cares about the label. This is: • Simpler (no need to go into Disk Management → Change Drive Letter & Paths every time). • More robust (if Windows can’t give you D:, it might give you E: instead—your script still finds it by label).
💡 7) (Optional) Forcing a Specific Drive Letter
If you really did want Card #1 always to mount as E:\ and Card #2 always as F:, you could: 1. Attach each card one at a time. 2. Open Disk Management (diskmgmt.msc) → right‐click that volume → “Change Drive Letter and Paths…” → Assign a static letter (E:\ or F:).
But: • If both cards end up in the same reader slot, Windows will re‐use the same mount point (D:, or whichever you let it). • Assigning persistent letters is mostly useful if you have multiple readers or multiple slots. Since you only ever have one card in the USB reader at a time, static letters are not strictly necessary.
🔄 8) Final Checklist 1. Ensure each XQD card’s VolumeLabel is unique (e.g. XQD_Card1, XQD_Card2). 2. Save the “label-aware” script above to C:\Scripts\my_xqd_smb_share.ps1. 3. Run the Register-ScheduledTask block (as Administrator) so that your script is invoked on every hotplug event. 4. Unplug any card currently in the reader, then plug in Card #1 → confirm \\XQD_Card1 appears on your LAN. 5. Eject Card #1, then plug in Card #2 → confirm \\XQD_Card2 appears.
After that, no matter which USB port you use (or which drive letter Windows picks), each card will be shared under its own unique network share name.