Category: Scripting

Scheduled Task for Reboot in Windows
article #1484, updated 66 days ago

The code below, deletes any past scheduled tasks of the name it uses, then sets up a new scheduled task for 3AM next, where it will run:

shutdown -f -r -t -0

and it does it using the SYSTEM account, whether Windows considers SYSTEM logged in or not, with the highest privileges.

# Delete old scheduled task of this name, if present

Unregister-ScheduledTask -TaskName "schtasks_REBOOT" -Confirm:$false 2> $null

# Create new scheduled task of this name

$action = New-ScheduledTaskAction -Execute 'C:\Windows\System32\shutdown.exe' -Argument '-f -r -t 0'
$today = Get-Date
$tomorrow3AM = Get-Date -Day ($today.day + 1) -Hour 3 -Minute 0 -Second 0
$trigger = New-ScheduledTaskTrigger -Once -At $tomorrow3AM
$taskprincipal = New-ScheduledTaskPrincipal -UserID 'NT AUTHORITY\SYSTEM' -RunLevel Highest
Register-ScheduledTask -TaskName 'schtasks_REBOOT' -Action $action -Trigger $trigger -Description 'Scheduled Reboot' -Principal $taskprincipal 

Categories:      

==============

Logoff all users from Windows
article #1478, updated 115 days ago

This will log off all users, whether console or RDP:

logoff console
quser /server:localhost | ForEach-Object {
		logoff $_.ID
		}

Categories:      

==============

Scripting a Universal Popup
article #1436, updated 441 days ago

Every scripting language which runs on Windows can call multiple methods of bringing up a popup to users. However, only this CMD method seems to do it for all users, and be callable universally:

msg * /time:9999 /v /w Put your message here!

Categories:      

==============

Get Windows version info in Powershell
article #1338, updated 1076 days ago

Here’s a great place to start:

Get-CimInstance -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption

This gets profoundly useful strings like “Microsoft Windows 10 Enterprise”. If you need the numeric version, the best so far has been:

(Get-ItemProperty -Path c:\windows\system32\hal.dll).VersionInfo.FileVersion

which, right now on this machine, gets us “10.0.18362.387 (WinBuild.160101.0800)”. And for the Windows 10 build:

(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name ReleaseID -ErrorAction Stop).ReleaseID

which gets “1903”. All of these are fast, do not depend on systeminfo, and appear to be nice and reliable.

Categories:      

==============

Silent Install of Adobe Reader DC
article #1318, updated 1125 days ago

First, download the EXE here:

https://get.adobe.com/reader/enterprise/

Then cause it to run thus:

AcroRdrDC1901220036_en_US.exe /sAll /rs /rps

A full list of command-line options can be had with /?.

Categories:      

==============

Rick's Super-Duper CMD Code Accelerator
article #1306, updated 1188 days ago

This one is from the amazing Rick Boatright. I saw the ancestor of this thirty-plus years ago in Unix System V, had no idea it had gotten so useful in Microsoft-land. The gist of it is:

  • You have a batch file, and want to access something involving a UNC path, something like this: \\SERVER_NAME\share_name\dir1\dir2
  • Default logic often involves storage of current location into a variable, CD, resumption of previous, blah, blah, blah.
  • But we can do it in one command: pushd \\SERVER_NAME\share_name\dir1\dir2 This does multiple things:
  • First, it creates a temporary drive letter for the server and share name. It chooses an available drive letter.
  • Secondly, without any further ado, it changes the current working directory of the shell (of the script) to the very location you pointed at.
  • So, if you did the pushd above, and if Z: were available, your current working directory suddenly becomes: Z:\dir1\dir2
    where Z: is mapped to \\SERVER_NAME\share_name !!!
  • Then when you’re done with it, just put in popd, and Z: goes away and you’re back to the current working directory you had beforehand!

Categories:      

==============

Close shared files on a Windows file server in Powershell
article #1288, updated 1246 days ago

Rather good to see:

https://docs.microsoft.com/en-us/powershell/module/smbshare/close-smbopenfile

Categories:      

==============

Pin to Taskbar for All Users in Windows
article #1287, updated 1249 days ago

So we have a terminal server or other multi-user Windows machine, Windows 7/2008R2 or later. We want to pin one or more icons to the taskbar, for all users. We discover that this is not something extremely easy to do :-) We can, at least reasonably easily, set up the same taskbar icon set for all users, thusly:

  1. Log into the one machine, and set up the taskbar as you would like it to appear for all.
  2. Export the following registry key:
    [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband]
    to a file named TaskBarPins.REG. Put it in a permanent folder outside of user space, e.g. C:\AutoSettings, it will be imported automatically at every login.
  3. Create SetTaskBarPins.VBS in C:\AutoSettings, containing the following text:
Option Explicit
On Error Resume Next
Dim objShell, ProgramFiles, sRegFile
Set objShell = CreateObject("WScript.Shell")
sRegFile = """C:\AutoSettings\SetTaskBarPins.REG"""
objShell.Run "Regedit.exe /s " & sRegFile, 0, True
Set objShell = Nothing
  1. Create a shortcut to C:\AutoSettings\SetTaskBarPins.VBS in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.

The next time any user logs into this machine, nothing will appear to have been changed. But when they log off and then log on after that, their taskbar will be the same as the one you exported.

Categories:      

==============

Run PowerShell script bypassing execution policy
article #1131, updated 1746 days ago

Run it like this, from CMD:

"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass script.ps1

Categories:      

==============

AutoHotKey and VBscript self-elevate to administrator
article #1104, updated 1823 days ago

There is a way:

https://autohotkey.com/docs/commands/Run.htm#RunAs

http://www.kellestine.com/self-elevate-vbscript/

Categories: