$desktopPath = [Environment]::GetFolderPath('Desktop') Register-WmiEvent -Class Win32_VolumeChangeEvent -SourceIdentifier USBEvent -Action { $eventType = $event.SourceEventArgs.NewEvent.EventType $driveLetter = $event.SourceEventArgs.NewEvent.DriveName.Substring(0, 1) $driveLabel = (Get-Volume -DriveLetter $driveLetter).FileSystemLabel if ($eventType -eq 2) { # Conexión de un dispositivo USB CreateShortcut $driveLetter $driveLabel } elseif ($eventType -eq 3) { # Desconexión de un dispositivo USB RemoveShortcut $driveLetter $driveLabel } } function CreateShortcut($driveLetter, $driveLabel) { $shortcutFile = Join-Path -Path $desktopPath -ChildPath "$driveLetter - $driveLabel.lnk" $shell = New-Object -ComObject Shell.Application $desktop = $shell.Namespace(0x10) $drive = $shell.Namespace($driveLetter + ':') $shortcut = $desktop.ParseName($shortcutFile) if ($shortcut -eq $null) { $shortcut = $desktop.Items().Item($driveLetter + ' - ' + $driveLabel + '.lnk') if ($shortcut -eq $null) { $shortcut = $desktop.Self.CreateShortcut($shortcutFile) $shortcut.Target.Path = $drive.Self.Path $shortcut.Save() } } } function RemoveShortcut($driveLetter, $driveLabel) { $shortcutFile = Join-Path -Path $desktopPath -ChildPath "$driveLetter - $driveLabel.lnk" if (Test-Path $shortcutFile) { Remove-Item $shortcutFile -Force } } while ($true) { Start-Sleep -Seconds 1 }