I need a help powershell script

Hello all,

I need a script powershell for forcing use of locking session security after 30 seconds of inactivity please

By coincidence, I recently wanted to write almost exactly the same script. Here is my version. See if it works for you.

# Define the inactivity limit in seconds
$InactivityLimitSeconds = 30

# Get the current value of the policy
$currentPolicy = Get-WmiObject -Class Win32_ComputerSetting -Namespace "root\cimv2\security\microsofttpm" | Where-Object { $_.keyname -eq "EnableScreenSaver" }

# Check if the current value is different from the desired one
if ($currentPolicy.settingvalue -ne $InactivityLimitSeconds) {
    # Set the new value for the policy
    Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name "InactivityTimeoutSecs" -Value $InactivityLimitSeconds

    Write-Host "Session lock after $InactivityLimitSeconds seconds of inactivity has been enforced."
} else {
    Write-Host "Session lock after $InactivityLimitSeconds seconds of inactivity is already enforced."

I also have this script here:

# Import .NET assembly for setting screen saver
Add-Type -AssemblyName System.Windows.Forms

# Lock screen feature
function Lock-Screen {
    # lock screen
    [System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend, $false, $false)

# Timer für Maus-Inaktivität (in Sekunden)
$InactiveTimeSeconds = 30

# Creating a timer for mouse inactivity
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = $InactiveTimeSeconds * 1000  # milliseconds
$timer.Enabled = $true

         # If the mouse cursor was moved, reset the timer
    if ([System.Windows.Forms.Cursor]::Position -ne $global:LastMousePosition) {
        $global:LastMousePosition = [System.Windows.Forms.Cursor]::Position
    # When the timer expires, lock the screen
    else {

# Save the current mouse position
$global:LastMousePosition = [System.Windows.Forms.Cursor]::Position

# start script
Write-Host "The script to lock the screen after $InactiveTimeSeconds seconds of mouse inactivity has started. Press Ctrl+C to exit the script."

# Keep the script running
try {
    while ($true) {
        # Keep the script running
        Start-Sleep -Seconds 1
finally {
    # Clear the timer when the script exits

But this script will only track the inactivity time of your mouse…

Thanks a lot for your response, the script is walking. But I execute it by a UEM so when I want to know if I can delete the start script part. You will find below a screenshot

Apologies, but to set expectations, I need to respond to this and mention that this forum isn’t for writing scripts for others, it is for getting help. Therefore, instead of making a request for a script, try solving the problem yourself and sharing your code, and explain what issues you are having. That’s not to say that others may not hop in and do it anyway, but that’s not the purpose of these forums, and I need to ensure a statement is made to align with purpose.

I’m sorry, :sweat_smile: I wouldn’t have written any code for him, but I happened to have a similar script lying around.

@Ladebrouille You heard him… If you have problems with your code, maybe create a new thread :sweat_smile:

No you’re fine! I just don’t want to set a precedence where someone thinks because a person happened to share a script, that they can post again and ask for the same thing, expecting someone to do it, then gets upset when people say sorry, no :slight_smile:. Hopefully that makes sense!

1 Like

I have finally fix the issue. Thanks for your help

1 Like

sorry it’s my first day on this forum

How about you share what you did to resolve the issue so that others can learn from your experience?

wdym? There was no issue, I just posted an old script I had to track the inactivity time…?

Color me stupid, but I did a google search on Win32_ComputerSetting and it had ZERO results. I even caved and gave Bing a try, nothing useful there either. Where did you find that code and how did it ever work? Just curious. I have never seen that WMI class nor that NameSpace.

It actually doesn’t exist :sweat_smile:
Sometimes when I don’t know any specific code, I “invent” placeholders for myself, maybe I should have written about it, sorry.

The script actually didn’t work for me either, but I thought maybe it would help the person making the request since they didn’t have any code.

Good to know, thanks for the update :slight_smile:

You should clearly point this out in your post to prevent the questioner or any future reader from wasting time trying to make your code work. :point_up:t3:

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