# Run job not only in current session

**URL:** <https://forums.powershell.org/t/run-job-not-only-in-current-session/23009>\
**Category:** PowerShell Help\
**Created:** [October 18, 2023, 5:08pm UTC](https://forums.powershell.org/t/run-job-not-only-in-current-session/23009 "2023-10-18T17:08:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruslan.m.huseynov](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@ruslan.m.huseynov](https://forums.powershell.org/u/ruslan.m.huseynov)\
**Post date:** [October 18, 2023, 5:08pm UTC](https://forums.powershell.org/t/run-job-not-only-in-current-session/23009/1 "2023-10-18T17:08:51Z")

</div>

Good day. I have a script fragment:  
#Query that will run when a USB device is plugged  
$query1 = “SELECT \* FROM \_\_InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA ‘Win32\_PnPEntity’ AND TargetInstance.Description LIKE ‘%FIDO%’”  
Register-WmiEvent -Query $query1 -Action {  
“FIDO” | Set-Content -Path “$env:TEMP\FIDOWitnessToken2.txt”  
}

# Query will listen to unplug event

$query = “SELECT \* FROM \_\_InstanceDeletionEvent WITHIN 5 WHERE TargetInstance ISA ‘Win32\_PnPEntity’ AND TargetInstance.Description LIKE ‘%FIDO%’”  
Register-WmiEvent -Query $query -Action {  
If (Test-Path “$env:TEMP\FIDOWitnessToken2.txt”) {  
rundll32.exe user32.dll,LockWorkStation  
Remove-Item “$env:TEMP\FIDOWitnessToken2.txt”  
}  
}

It works when powershell cmd is opened. How can i make it work at any time? For example it runs as service never stops.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [October 18, 2023, 5:48pm UTC](https://forums.powershell.org/t/run-job-not-only-in-current-session/23009/2 "2023-10-18T17:48:17Z")

</div>

Hey there and welcome!

Could you go back and edit your post and and format your code?

See: [How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif)  
Sometimes the _preformatted text_ button hides behind the settings gear symbol

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [October 18, 2023, 5:57pm UTC](https://forums.powershell.org/t/run-job-not-only-in-current-session/23009/3 "2023-10-18T17:57:15Z")

</div>

As far as an answer to your question, typically my go to for something like this is a scheduled task. Assuming USB connections are monitored, you should be able to find a corresponding log event that happens and use that as a trigger. It might not be enabled by default either, so you may have to enable it on the system(s) you’re wanting that info. This might get you started: [How to track down USB flash drive usage with Windows 10’s Event Viewer | TechRepublic](https://www.techrepublic.com/article/how-to-track-down-usb-flash-drive-usage-in-windows-10s-event-viewer/) and once you can confirm you can trigger on a specific log, you should be able to set the trigger of scheduled task to ‘on an event’ and go from there. Looks like 2100 or 2102 are the events that you’d likely be looking for.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 7, 2024, 6:33am UTC](https://forums.powershell.org/t/run-job-not-only-in-current-session/23009/4 "2024-02-07T06:33:19Z")

</div>

Closed on February 7, 2024, 6:33am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
