# Minor problem running script

**URL:** <https://forums.powershell.org/t/minor-problem-running-script/25360>\
**Category:** PowerShell Help\
**Created:** [January 24, 2025, 2:40pm UTC](https://forums.powershell.org/t/minor-problem-running-script/25360 "2025-01-24T14:40:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kkoop](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kkoop](https://forums.powershell.org/u/kkoop)\
**Post date:** [January 24, 2025, 2:40pm UTC](https://forums.powershell.org/t/minor-problem-running-script/25360/1 "2025-01-24T14:40:59Z")

</div>

I have a PS script which checks a collection of servers to determine if my admin account is logged in - I use Team Viewer RDP sessions for a lot of my work. When it finds a session, it logs my admin account out of that server. I use this at the end of the day so that I don’t leave my admin account logged in somewhere I overlooked.

If I open a PS window and launch the script, it appears to hit all the servers in my list but doesn’t do anything.

If I open PS “as administrator” and run the script, it logs me out of the sessions it finds. So far, so good.

Here’s my code - note that Write-Color is a function I did not include here.

```auto
$Username = "MyAdminAccountID"
$serverList = get-content c:\utils\ServersKLst.csv
$StartTime = $(get-date)
$LogCount = 0
foreach ($Server in $serverList) {
			$sessionID = ((cmd /c quser /server:$server "2>NUL"| ? { $_ -match $username }) -split ' +')[2]
			Write-Color -text "$sessionID"," $Server" -color yellow,yellow
				If ($sessionID)	{
					Write-color -text " $($Username)"," logged ","OFF ","$($Server)"," session ID: ","$($sessionID)" -color blue,green,red,yellow,green,yellow
					Logoff $sessionID /server:$Server
					$LogCount++
				}
	}
	
$elapsedTime = $(get-date) - $StartTime
$totalTime = "{0:HH:mm:ss}" -f ([datetime]$elapsedTime.Ticks)
if ($LogCount -ne 1) {
	$Plural = "s"
	}
Write-Color -text "`n Logoff completed. Total time:"," $totalTime"," Logged off of ","$LogCount"," Server$Plural`n`n" -color cyan,yellow,green,yellow,green
Pause

```

So, to use this I have to:

- right-click Powershell
- select Run As Admin
- Enter my Admin creds
- Change directory to C:\Utils
- Run .\logmeoff.ps1

What I’d like to do is have an icon on my desktop (or preferably task bar) which I can run at the end of the day as I’m packing up and have this script close any sessions I haven’t already closed. If I have to provide my admin creds when I launch the desktop icon that’s fine. I just don’t know how to do this.

Thanks for suggestions!

—K

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [January 24, 2025, 3:38pm UTC](https://forums.powershell.org/t/minor-problem-running-script/25360/2 "2025-01-24T15:38:41Z")

</div>

You can create a new shortcut file, then fill in the details on the Shortcut tab like so:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/e/ea178cb15e8a118c624d9efa9016d9d2cbcbfffd.png)  
The target should be something like:

```auto
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -file "C:\Utils\logmeoff.ps1"

```

I don’t think it’s necessary to change the “Star in” field, but you can.  
Then click the advanced button and check the box that says you want it to run as administrator. Finally, click the Change Icon button and select whatever icon you want. I went with the Powershell/UAC combo icon.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [January 24, 2025, 3:56pm UTC](https://forums.powershell.org/t/minor-problem-running-script/25360/3 "2025-01-24T15:56:46Z")

</div>

You’ll also need to go into Advanced and tick ‘Run as admin’ since your regular user doesn’t have permission to the quser command on the remote systems

---

<div class="post-metadata">

**Author:** ![kkoop](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kkoop](https://forums.powershell.org/u/kkoop)\
**Post date:** [January 24, 2025, 4:06pm UTC](https://forums.powershell.org/t/minor-problem-running-script/25360/4 "2025-01-24T16:06:29Z")

</div>

Thanks, Grey! That worked perfectly.

Much appreciated

---

<div class="post-metadata">

**Author:** ![kkoop](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kkoop](https://forums.powershell.org/u/kkoop)\
**Post date:** [January 24, 2025, 4:07pm UTC](https://forums.powershell.org/t/minor-problem-running-script/25360/5 "2025-01-24T16:07:11Z")

</div>

neemobeer, Grey mentioned that in the instructions

Thanks!

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [January 24, 2025, 4:11pm UTC](https://forums.powershell.org/t/minor-problem-running-script/25360/6 "2025-01-24T16:11:33Z")

</div>

Heh heh, missed that 😬

---

<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 23, 2025, 4:11pm UTC](https://forums.powershell.org/t/minor-problem-running-script/25360/7 "2025-02-23T16:11:45Z")

</div>

Closed on February 23, 2025, 4:11pm UTC

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