Skip to content

Instantly share code, notes, and snippets.

@codeartery
codeartery / RunAsAdmin.vbs
Created January 6, 2021 16:23
Will run the current VBScript as an administrator.
REM:<RunAsAdmin>
' Place this code at the top of your VBScript file to run the script as an admin.
' It will quit the current script and re-run the script as an administrator.
' Note that depending on your UAC permissions you may be prompted.
' Does not support command line arguments.
If Not WScript.Arguments.Named.Exists("AsAdmin") Then
CreateObject("Shell.Application").ShellExecute "WScript.exe", """" & WScript.ScriptFullName & """ /AsAdmin", "", "runas", 1
WScript.Quit
End If
REM:</RunAsAdmin>
@codeartery
codeartery / RunAsAdminNoUAC.vbs
Last active July 21, 2024 05:31
Will run the current VBScript as an administrator without a UAC prompt after the initial setup run.
REM:<RunAsAdminNoUAC>
' Place this code at the top of your VBScript file you'd like to run with admin permissions.
' When you first run it it will ask for UAC permissions and run and add a task to your Windows 'Task Scheduler' with the name of the script.
' From here on out the VBScript file name and location should not be changed.
' Next time you run it, it will be called from the Task Scheduler with admin permissions and thus not prompt for UAC anymore.
' This script runs with WScript.exe, and does not support command line arguments.
' To delete the task, use the Windows Task Scheduler.
With CreateObject("WScript.Shell")
If WScript.Arguments.Named.Exists("CreateTask") Then
.Run "schtasks /Create /SC ONCE /TN """ & WScript.ScriptName & """ /TR ""wscript.exe \""" & WScript.ScriptFullName & "\"" /AsAdmin"" /ST 00:01 /IT /F /RL HIGHEST", 0, True
@codeartery
codeartery / Edit-Item.ps1
Last active July 21, 2024 05:31
Very simple 'console' text editor for quick file edits.
function Edit-Item {
<#
.SYNOPSIS
Sendkeys the contents of $Path into a here string in the current console for quick simple edits. Should not be used on large files.
.EXAMPLE
edit file.txt
#>
[CmdletBinding()]
param (
@codeartery
codeartery / Invoke-Sql.ps1
Last active January 5, 2021 22:34
Executes and returns the results of an SQL query in PowerShell.
function Invoke-Sql {
<#
.SYNOPSIS
Executes and returns the results of an SQL query.
.EXAMPLE
$results = Invoke-Sql -ConnectionString "Server=TEST-SERVER\SQLEXPRESS;Database=Test_DB;Integrated Security=True" -Query "SELECT * FROM Employee"
Calls an SQL query and saves the results.
@codeartery
codeartery / carousel.js
Last active April 22, 2023 05:46
A pure JavaScript Carousel/Slideshow.
@codeartery
codeartery / BrowseForFile.vbs
Last active September 8, 2024 10:19
Browse for file dialog in VBScript that allows filtering by type.
Function BrowseForFile()
REM@description
' HTML based browse for file dialog that doesn't require a temporary file.
REM@returns
' BrowseForFile <string> - The file path of the selected file.
REM@author
' Jeremy England, http://codeartery.com/
REM@mini
' Function BrowseForFile():BrowseForFile=CreateObject("WScript.Shell").Exec("mshta.exe ""about:<input type=file id=f><script>resizeTo(0,0);f.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(f.value);close();</script>""").StdOut.ReadLine():End Function
@codeartery
codeartery / IsProcessRunning.vbs
Last active August 21, 2024 06:13
Check if a process is running in VBScript.
Function IsProcessRunning( processNameExe )
IsProcessRunning = GetObject("WinMgmts:\\.\root\cimv2") _
.ExecQuery("SELECT * FROM Win32_Process WHERE Name LIKE '" & processNameExe & "'") _
.Count > 0
End Function
@codeartery
codeartery / FormatDateAs.vbs
Last active April 27, 2023 12:33
Format a date based off of a user defined 'd', 'm', 'y' pattern in VBScript.
Function FormatDateAs( dInput, sFormat, padZero )
REM@description
' Format a date based off of a user defined 'd', 'm', 'y' pattern.
REM@params
' dInput <date> - The date to format.
' sFormat <string> - The 'd', 'm', 'y' format you want the date in.
' padZero <bool> - Pads blank spaces with zeros when extra 'd', 'm', or, 'y's are provided than needed.
REM@returns
' FormatDateAs <string> - The formatted date.
REM@author
@codeartery
codeartery / CmdOut.vbs
Last active July 21, 2024 05:32
Run a command prompt command and get its output in VBScript.
Function CmdOut( pCmd )
REM@description
' Run a command prompt command and get its output.
REM@params
' pCmd <string> - A command prompt command.
REM@returns
' CmdOut <string> - The output of the command.
REM@author
' Jeremy England, http://codeartery.com/
REM@mini
@codeartery
codeartery / Import.vbs
Last active July 21, 2024 05:32
Import/include/using code from an external VBScript file.
Function Import( vbsFile )
REM@description
' Import/include/using code from an external VBScript file.
REM@author
' Jeremy England, http://codeartery.com/
REM@params
' vbsFile <string> - A relative, absolute, or URL path to a file containing vbscript code.
REM@returns
' Import <bool> - Returns False if the import failed, and True if it succeeded.
REM@mini