Showing posts with label Batch. Show all posts
Showing posts with label Batch. Show all posts

Apr 15, 2025

Windows 10/11 : Batch File to Start and Check Windows Services

Create a batch file to check the status of a Windows Service(s) and start it if its not started. The batch file can also be run inside "Task Scheduler" at an interval basis.


1. First, you need to find out the Service Name that you want to monitor, in this example let's say the service you need to monitor is an Exchange Server IMAP4 service.


2. Open --> Services and browse through the list until you found the IMAP4 service.


3. Right-click the Service Name --> Properties.


4. Take note of the "Service name:", copy the name to Notepad. As per below screenshot, the service name is "MSExchangeImap4".



5. Create a batch file as below :-


@ECHO OFF
CLS
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A RETRY_IMAP=0
SET /A MAX_RETRIES=3
SET EXCH_IMAP= MSExchangeImap4

REM To check current status of the service.
:check_imap
SC QUERY %EXCH_IMAP% | FIND "RUNNING" >NUL
IF %ERRORLEVEL%==0 (
    ECHO Exchange IMAP4 Service is Running.
        GOTO end
)

REM To start the service if the status is stopped.
ECHO Exchange IMAP4 Service is Stopped.
SC START %EXCH_IMAP%
TIMEOUT /T 10 >NUL

REM To re-check the service status again for confirmation.
SC QUERY %EXCH_IMAP% | FIND "RUNNING" >NUL
IF %ERRORLEVEL%==0 (
    ECHO Exchange IMAP4 Service is Running.
        GOTO end
)

REM Retrying to start the service with a maximum of 3 retries only.
SET /A RETRY_IMAP+=1
IF !RETRY_IMAP! LSS %MAX_RETRIES% (
    ECHO Retrying Start %EXCH_IMAP% (Attemp: !RETRY_IMAP!).
    GOTO check_imap
) ELSE (
    ECHO Max Retries Reached for %EXCH_IMAP%, will stop retrying.
        GOTO end
)

:end
EXIT



6. You can now test run the batch file and create a "Task Scheduler" to run the check at an interval basis (eg. every 2 hours or at Startup of the server).



!!! HAPPY COMPUTING !!!

Oct 3, 2024

Command : Rename Multiple Files via Batch Script

Rename multiple files via batch script. Some times you need to rename multiple files and instead of renaming each file individually or relying on some 3rd party apps, you can now just use a simple batch script to achieve the same results.




@echo off
setlocal enabledelayedexpansion

set prefix="[Filename]"
set count=1

for %%f in (*.[Extension]) do (
    set formattedCount=00!count!
    set formattedCount=!formattedCount:~2!

    ren "%%f" "!prefix!!formattedCount!.[Extension]"
    set /a count+=1
)

echo Done renaming files.
pause



!!! HAPPY COMPUTING !!!

Nov 8, 2023

Batch : Rename Hostname via Batch File

This is an example of a batch file to rename the Hostname or Computer Name as per your preferences, it is much faster way to do computer renaming for many computers.

Note that the batch file must be "run as administrator" mode in order to works.

 @ECHO OFF
CLS
>NUL CHCP 65001

:asktorename
REM To ask whether to Rename the Hostname or not.
ECHO.
ECHO.
ECHO.
ECHO               ╔═════════════════════════════════════════╗
ECHO               ║         RENAMING THE HOSTNAME           ║
ECHO               ╚═════════════════════════════════════════╝
ECHO.
CHOICE /M "──────────────► DO YOU WANT TO RENAME THE HOSTNAME "
    IF ERRORLEVEL 2 GOTO eof
    IF ERRORLEVEL 1 GOTO askforname
GOTO eof
REM --------------------------------------------------------------------------------------

:askforname
REM To ask User for the preferred hostname.
ECHO.
ECHO.
SET /P NEWNAME="PLEASE TYPE THE NEW HOSTNAME: "
ECHO.
ECHO "     ► ► ► THE NEW HOSTNAME IS ───► %NEWNAME%"
CHOICE /M "     ► ► ► IS THIS CORRECT "
    IF ERRORLEVEL 2 GOTO asktorename
    IF ERRORLEVEL 1 GOTO dorenhost
GOTO eof
REM --------------------------------------------------------------------------------------

:dorenhost
REM To proceed Rename the Hostname as per Input by User.
ECHO.
ECHO "     ► ► ► OK, RENAMING HOSTNAME → %NEWNAME%, PLEASE WAIT..."
WMIC COMPUTERSYSTEM where name="%COMPUTERNAME%" CALL RENAME name="%NEWNAME%"
TIMEOUT /t 3 /NOBREAK
ECHO.
ECHO "     ► ► ► RENAMING HOSTNAME COMPLETE"
ECHO.
ECHO "THE NEW HOSTNAME WILL TAKE EFFECT AFTER COMPUTER RESTART"
TIMEOUT /t 5 /NOBREAK
GOTO eof
REM --------------------------------------------------------------------------------------

:eof
START SHUTDOWN /r /f /t 10
COLOR
>NUL CHCP 437
EXIT /b



!!! HAPPY COMPUTING !!!

Jan 6, 2023

PowerShell : Command Line

PowerShell Command for Windows 10 22H2. 

1) Enable/Install SMB v1 Client Protocol.

Enable-WindowsOptionalFeature -Online -FeatureName "SMB1Protocol-Clinet" -All


2) Mount ISO image file.

Mount-DiskImage -ImagePath "%ISO_FILE_PATH%"


3) Unmount/Eject ISO Image file.

Dismount-DiskImage -ImagePath "%ISO_FILE_PATH%"


4) To run a PowerShell Script File (.ps1) inside a Batch File.

POWERSHELL.EXE -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%POWERSHELL_SCRIPT_FILE%""' -Verb RunAs}"


!!! HAPPY COMPUTING !!!


Jan 5, 2023

Batch : Rename Hostname

 Inside batch file, create the following command line to rename the current computer with a new hostname.

WMIC COMPUTERSYSTEM where name="%COMPUTERNAME%" CALL RENAME name="%NEWPCNAME%"

Need to restart computer to take effect.


!!! HAPPY COMPUTING !!!


Apr 27, 2022

Batch - IF Calculation

 Batch File Command "IF"

  • EQU = Equal to...
  • NEQ = Not Equal to...
  • LSS = Less than...
  • LEQ = Less than or Equal to...
  • GTR = Greater than...
  • GEQ = Greater than or Equal to...
Example :-

IF %INPUT% EQU 100 ECHO "EQUAL VALUE"
IF %INPUT% GEQ 100 ECHO "VALUE IS GREATER THAN OR EQUAL TO 100"

Feb 5, 2017

Windows 10 - Remove Provisioned Apps

This is my second post about Windows 10 built-in apps (Provisioned Apps), some of us just don't like the built-in apps... just like the ridiculous Xbox apps which I find it to be best at hogging system resources.

Now my last post was to remove those built-in apps, but later I found it was still in Windows 10 when another user logged-in. After much googling and studying MSDN and TechNet, I found a powershell script and I wrote some sample of my own.

First of all, you still need to run the previous powershell script to ensure that all users are not provisioned with the built-in apps, just in case I have re-posted those script here for your references.


Get-AppxPackage -allusers *3dbuilder* | Remove-AppxPackage
Get-AppxPackage -allusers *windowsalarms* | Remove-AppxPackage
Get-AppxPackage -allusers *zunevideo* | Remove-AppxPackage
Get-AppxPackage -allusers *officehub* | Remove-AppxPackage
Get-AppxPackage -allusers *skypeapp* | Remove-AppxPackage
Get-AppxPackage -allusers *getstarted* | Remove-AppxPackage
Get-AppxPackage -allusers *zunemusic* | Remove-AppxPackage
Get-AppxPackage -allusers *solitairecollection* | Remove-AppxPackage
Get-AppxPackage -allusers *bingfinance* | Remove-AppxPackage
Get-AppxPackage -allusers *bingnews* | Remove-AppxPackage
Get-AppxPackage -allusers *people* | Remove-AppxPackage
Get-AppxPackage -allusers *windowsphone* | Remove-AppxPackage
Get-AppxPackage -allusers *commsphone* | Remove-AppxPackage
Get-AppxPackage -allusers *bingsports* | Remove-AppxPackage
Get-AppxPackage -allusers *windowsstore* | Remove-AppxPackage
Get-AppxPackage -allusers *bingweather* | Remove-AppxPackage
Get-AppxPackage -allusers *xboxapp* | Remove-AppxPackage
Get-AppxPackage -allusers *twitter* | Remove-AppxPackage
Get-AppxPackage -allusers *office.sway* | Remove-AppxPackage
Get-AppxPackage -allusers *xboxonesmartglass* | Remove-AppxPackage
Get-AppxPackage -allusers *messaging* | Remove-AppxPackage

Note : You can choose which apps you want to retain back. 

After you have completed the above script, now you can run the 2nd script as below, also you can choose which apps you want to retain back.


Remove-AppxProvisionedPackage -online -packagename Microsoft.3DBuilder_11.0.47.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.BingWeather_4.9.51.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.Getstarted_3.11.3.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.Messaging_2.7.1001.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.MicrosoftOfficeHub_2015.6801.23751.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.MicrosoftSolitaireCollection_3.9.5100.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.Office.OneNote_2015.6868.57981.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.OneConnect_1.1605.17.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.People_2016.709.155.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.People_2016.709.155.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.SkypeApp_11.4.86.0_neutral_~_kzf8qxf38zg5c
Remove-AppxProvisionedPackage -online -packagename Microsoft.StorePurchaseApp_1.0.454.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.WindowsAlarms_2016.622.2118.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename microsoft.windowscommunicationsapps_2015.6868.41201.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.WindowsFeedbackHub_1.3.1741.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.WindowsMaps_2016.701.2235.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.WindowsStore_11606.1001.394.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.XboxApp_2016.623.248.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.XboxIdentityProvider_2016.616.818.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.ZuneMusic_2019.6.19261.0_neutral_~_8wekyb3d8bbwe
Remove-AppxProvisionedPackage -online -packagename Microsoft.ZuneVideo_2019.6.19281.0_neutral_~_8wekyb3d8bbwe

 
After completion, you just need to restart the computer. Please be aware that if an apps was still provisioned to a user, you cannot remove that apps. As such it is important that you ensure no users is still requiring that apps before removing.

I find this to be useful especially when you want to create a consistent operating environment (COE) for a corporate/enterprise system.

!!! HAPPY SCRIPTING !!!
!!! HAPPY CHINESE NEW YEAR !!!

Oct 6, 2015

Batch - Batch to EXE Converter

Well as the saying goes "All good things must be share..." and so I'm sharing a tools that I find it very useful and convenient.

Ever created a batch file (.bat) that you don't want other people to mess with it ? Or simply you just wanted to hide the commands ?

There's a lot of free tools available when you google it but none are free for commercial use or there's no limitation on usage. I'm recommending using F2KO utility to convert your batch file into an .exe file format.

It's very simple to use and no installations required, do give it a try and there's others of cool stuff to in his website.

Jun 22, 2015

Batch - Join Domain Batch File

This is a batch file to automatically join the computer to the specified domain, the purposes of the batch file as below :-

  • To avoid typography error in domain name.
  • To avoid mismatch username and password when joining to domain.
Syntax of the command :
 
netdom join %COMPUTERNAME% /domain:[domain name] /userd:[domain\username] /passwordd:[password]

 
  • %COMPUTERNAME% = Computer's Name, a variables.
  • [domain name] = The actual domain name (eg. google.com)
  • [domain\username] = The actual domain's username (eg. google.com\administrator)
  • [password] = The username's password.
Of course you can put in a lot of things into this batch file such as checking the computer's name validity (eg. length of the name, cross checking with a database file to match the S/N etc.)

Note that the "netdom" command is NOT AVAILABLE in standard Windows 7 OS, it must be installed separately. Download & install Microsoft Remote Server Administration Tools (RSAT).

Once installed you need to enable the "AD DS Command-Line Utility" before you can start using the "netdom" command.

Jun 1, 2015

Batch - Verify Model & Install Drivers

The following batch file are to check whether the supported model is valid or not and if valid will proceed to next step which is "valid" in this case.

Subsequent batch command is to call the HP SSM built-in command to install the respective drivers automatically.

 
REM Check for valid model & proceed to install.
SET INPUT=Model
for /f "tokens=1,* delims==" %%i IN ('wmic computersystem get %INPUT% /VALUE ^| FIND /I "%INPUT%="') do set Model=%%j
 
ECHO   Found the following model : %Model%
 
IF "%Model%" EQU "HP Compaq 6200 Pro SFF PC" GOTO valid
IF "%Model%" EQU "HP Compaq Pro 6300 SFF" GOTO valid
IF "%Model%" EQU "HP Elite 8300 USDT" GOTO valid
IF "%Model%" EQU "HP ProDesk 600 G1 SFF" GOTO valid
IF "%Model%" EQU "HP EliteDesk 800 G1 SFF" GOTO valid
IF "%Model%" EQU "HP EliteBook 2560p" GOTO valid
IF "%Model%" EQU "HP ProBook 6470b" GOTO valid
IF "%Model%" EQU "HP EliteBook 2570p" GOTO valid
IF "%Model%" EQU "HP EliteBook Folio 9470m" GOTO valid
IF "%Model%" EQU "HP ProBook 640 G1" GOTO valid
IF "%Model%" EQU "HP EliteBook 820 G1" GOTO valid
IF "%Model%" EQU "HP EliteBook 840 G1" GOTO valid
IF "%Model%" EQU "HP EliteBook 1040 G1" GOTO valid
IF "%Model%" EQU "HP Z230 SFF Workstation" GOTO valid

 
Please take note that the model name are {CASE SENSITIVE} and thus extra care required when typing the model name into the batch file.

May 29, 2015

Batch - Consistent Operating Environment

Consistent Operating Environment (COE), does anyone really understand what is this means ?

To my understanding, COE means that all computers must be running on a same OS, same applications/programs, same interface and same settings/configurations.

It is very hard achieve and yet simple to implement, the most troublesome thing is to make it compatible with all kind of models. Put it simple (using HP's model series and ranges), a single image to cater for almost all types of models yet the ability to install it's own drivers, applications etc. and make it function/working.

Luckily HP (Hewlett-Packard) does have a tools to help us in this tasks, but drawback would be custom scripting/batch file(s) have to be created & tested to ensure the workability of it.

It is called the SSM (Softpaq System Manager), what this tools does is to ensure the reliability of the downloaded drivers and the ability to differentiate the models being install and subsequently install the relevant drivers for the system.

I'm in the midst of compiling a "Universal Master Image" capable of installing the drivers base on the models detected and of course the customised settings required for the specific corporate environment.

Although I can't disclose much details of it, but I will post the scripts and hopefully might be usefull to someone.

May 26, 2015

MS-DOS - Batch File Color Codes

Syntax : COLOR [Background] [Foreground]

Colour attributes are specified by 2 of the following digits, each digit can be any value as below :-

0 = Black
8 = Gray

1 = Blue
9 = Light Blue

2 = Green
A = Light Green

3 = Aqua
B = Light Aqua

4 = Red
C = Light Red

5 = Purple
D = Light Purple

6 = Yellow
E = Light Yellow

7 = White
F = Bright White

Example : COLOR 0B

* A black background colour with Light Aqua text.