Hands-free with 5 VB scripts that automate the use of Windows computers

VB Script is scaled from Visual Basic programs used as Windows scripts can do anything from collecting important information about computer hardware to stopping and starting the service or reinstalling the card network.

Whether you are an IT expert or a regular computer user, there are many things you need to do regularly on your computer. VB Script is scaled from Visual Basic programs used as Windows scripts can do anything from collecting important information about computer hardware to stopping and starting the service or reinstalling the card network.

You can learn how to do all these personal things in the usual way or use batch files. But VB script is better than batch script because they are more flexible. If you store the following scripts in a common place for quick access when needed, you can complete these tasks in a short period of time, just by double-clicking the script, confirming Notice and task completed.

Take a look at the following Windows script VB and if you see any files you want to use, just copy and paste the script into Notepad or other encrypted note tool and save the file as a WSF file.

  1. These "hack" tips are only Notepad can do

Prepare Windows scripts

Each of the following scripts will run with just a double click on the condition that you have named the file with the extension .WSF and open the code with:



This ensures that Windows will recognize the language that your script has been written and handled correctly.

1. Use Windows script to find computer information

Windows provides a tool called WMI, or Windows Management Instrumentation, that provides scripts with an interface to access operating system components. You can run queries with WMI to get current information about the system. Microsoft provides a complete list of all types of queries that can be performed with the system.

Although VBA can be used to get computer information into Excel, you can do the same by using a simple VB script.

In this example, we will query the system to get processor information (model, manufacturer, core number), battery information (description and status), logical disk information (name, content). remaining amount, and overall size). Then we will export all this information to a CSV file for easy viewing.

The first step is to set up the FileSystemObject to use to export the CSV file and create the file:

Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile1 = "MyComputerInfo.csv"
Set oFile1 = oFSO.CreateTextFile(sFile1, 1)

The next step is to set up WMI queries and execute it:

 strQuery = "SELECT Family,Manufacturer,NumberOfCores FROM Win32_Processor" Set colResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery ) 

 

Finally, sort the results and include the information in the CSV file.

oFile1.WriteLine "Processor Information"
oFile1.WriteLine "------"

For Each objResult In colResults
strResults = "Family:,"+CStr(objResult.Family)
oFile1.WriteLine strResults
strResults = "Manufacturer:,"+CStr(objResult.Manufacturer)
oFile1.WriteLine strResults
strResults = "Number of Cores:,"+CStr(objResult.NumberOfCores)
oFile1.WriteLine strResults
Next

If you run the above code you will have the output like the image below:

Hands-free with 5 VB scripts that automate the use of Windows computers Picture 1Hands-free with 5 VB scripts that automate the use of Windows computers Picture 1

For the next two parts of the code, simply repeat and change the query to find additional information. This is querying battery information:

oFile1.WriteLine ""
strQuery = "SELECT Description,Status FROM Win32_Battery"
Set colResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
oFile1.WriteLine "Battery Information"
oFile1.WriteLine "------"

For Each objResult In colResults
strResults = "Status:,"+CStr(objResult.Description)
oFile1.WriteLine strResults
strResults = "Description:,"+CStr(objResult.Status)
oFile1.WriteLine strResults
Next

And this is the next part of the logical disk query:

oFile1.WriteLine ""
strQuery = "Select Name, FreeSpace, Size from Win32_LogicalDisk"
Set colResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
oFile1.WriteLine "Disk Information"
oFile1.WriteLine "------"

'Identify the Logical Disk Space
For Each objResult In colResults
strResults = "Name:,"+CStr(objResult.Name)
oFile1.WriteLine strResults
strResults = "Free Space:,"+CStr(objResult.FreeSpace)
oFile1.WriteLine strResults
strResults = "Disk Size:,"+CStr(objResult.Size)
oFile1.WriteLine strResults
Next

Finally, remember to close the code by closing the file and setting the objects to "Nothing":

oFile1.Close

Set oFile1 = Nothing
set colResults = Nothing
strResults = ""

Put all the code in the new .WSF file, then run and this is the result:

Hands-free with 5 VB scripts that automate the use of Windows computers Picture 2Hands-free with 5 VB scripts that automate the use of Windows computers Picture 2

Just change these queries with other computer devices or software that want to receive information and you can get full system reports at any time with just one click.

2. Stop and start the service

Sometimes specific services will have problems and require a quick restart to be able to run normally. If you want to quickly turn off and restart the service, you just need to save the following script. It will prompt the user to type the name of the service you want to reboot and the rest of it will do.

Since stopping and starting services requires special administrative rights, you need to place the following code when starting the script to grant advanced scripts to the script.

If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
WScript.Quit
End If

Once done, add the rest of the code to run the input box for user input, launch the command window and send "net stop" and "net start commands":

Set cmdShell = CreateObject("WScript.Shell")
strServiceName=Inputbox("Inter Service to Stop","Input Required")
cmdShell.Run "cmd.exe"
WScript.Sleep 1000

cmdShell.SendKeys "net stop "+strServiceName
cmdShell.SendKeys "{Enter}"
WScript.Sleep 1000

cmdShell.SendKeys "net start "+strServiceName
cmdShell.SendKeys "{Enter}"
WScript.Sleep 1000

cmdShell.SendKeys "Exit"
cmdShell.SendKeys "{Enter}"

That's it, you don't need to find service tools, just run scipt to stop and start any service in seconds.

3. Change registry settings, default admin password

With the script below, we can do a couple of things. This script will show you how to edit the registry with a VB script. It will also provide a script that allows you to set the default Windows username and password by editing those registry settings.

  1. Learn about the Windows Registry

This is how it works. First, because editing the registry requires admin rights, you need to set up advanced permissions for the script:

If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
WScript.Quit
End If

First run two input boxes to request the username and password:

strUserName=Inputbox("Enter the default User Name","Input Required")
strPassword=Inputbox("Enter the default Password","Input Required")

Next, set the object in the shell and write those values ​​into the appropriate registry keys:

Set wshShell = CreateObject( "WScript.Shell" )
wshShell.RegWrite "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionWinlogonDefaultUserName", strUserName, "REG_SZ"
wshShell.RegWrite "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionWinlogonDefaultPassword", strPassword, "REG_SZ"
Set wshShell = Nothing

The "RegWrite" method allows users to write any value for any registry key using a VB Windows script. All you need to know is the appropriate path.

Run the script and answer the messages that appear.

Hands-free with 5 VB scripts that automate the use of Windows computers Picture 3Hands-free with 5 VB scripts that automate the use of Windows computers Picture 3

The input values ​​will be inserted immediately into the registry settings you have set up in the script.

Hands-free with 5 VB scripts that automate the use of Windows computers Picture 4Hands-free with 5 VB scripts that automate the use of Windows computers Picture 4

4. Reset the network connection

Resetting the network connection using VB script will reset all active connections of the user to resolve any network problems that are encountered.

  1. This is how to reset network settings on Windows 10 with just one click

As with other scripts, this script also needs administrative privileges, you need to add the code in the script above to the top for advanced permissions. Next, create the WMI object and query it with a list of network adapters enabled on the system:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'True'")

Finally, go through all the enabled adapters and reset them:

For Each objItem in colItems
objItem.Disable
WScript.Sleep 1000
objItem.Enable
Next

This will reset all active network adapters, which is often the fastest way to resolve annoying network problems. Save this script and use it whenever you see slow networks and other unusual network problems.

5. Perform device ping or web page

This script is used to check if the website is working, then it will send the email to the user if the site has a problem. You can use this same script to monitor important servers or computers on the network and set up emailing you whenever the script can't ping the device.

  1. What is the PING command on the computer? How does it work?

First, set the script for the target to ping, create the shell object and then run the ping command.

strTarget = "topsecretwriters.com"
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & strTarget, 0, True)

Use a Select Case statement to run through the Ping results and respond accordingly. If the results return to zero, then the site (or server) is online and you do not have to do anything. If it returns "1" then the ping fails and you need to do something.

Select Case Ping
Case 0
Case 1
Set objMessage = CreateObject("CDO.Message")
Set objConfig = CreateObject("CDO.Configuration")
objConfig.Load -1
Set Flds = objConfig.Fields
With Flds
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="xxxxxx@gmail.com"
.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")="xxxxxxxxxxxxxxxxx"
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.gmail.com"
.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=465
.Update
End With
With objMessage
Set .Configuration = objConfig
.Subject = "Your site is offline"
.From = "me@mycomputer.com"
.To = "xxxxxx@gmail.com"
.TextBody = "Hey, your website is offline."
.Send
End With
End Select

When the script runs and cannot ping the device or website, you will receive an email.

Hands-free with 5 VB scripts that automate the use of Windows computers Picture 5Hands-free with 5 VB scripts that automate the use of Windows computers Picture 5

This way you can quickly and easily check your network.

These are just a few examples of some interesting things you can do with VB scripts to streamline your computer usage. There are many other things people can do like automatic backup with VB and Synctoy, automate telnet commands, or even open and control application windows.

See more:

  1. Managing Windows Networks Using Scripts - Part 1: Basic Concepts
  2. Use VBScript to remotely shutdown
  3. Automate TELNET commands using VB Script
5 ★ | 1 Vote