-->

ABOUT US

Our development agency is committed to providing you the best service.

OUR TEAM

The awesome people behind our brand ... and their life motto.

  • Kumar Atul Jaiswal

    Ethical Hacker

    Hacking is a Speed of Innovation And Technology with Romance.

  • Kumar Atul Jaiswal

    CEO Of Hacking Truth

    Loopholes are every major Security,Just need to Understand it well.

  • Kumar Atul Jaiswal

    Web Developer

    Techonology is the best way to Change Everything, like Mindset Goal.

OUR SKILLS

We pride ourselves with strong, flexible and top notch skills.

Marketing

Development 90%
Design 80%
Marketing 70%

Websites

Development 90%
Design 80%
Marketing 70%

PR

Development 90%
Design 80%
Marketing 70%

ACHIEVEMENTS

We help our clients integrate, analyze, and use their data to improve their business.

150

GREAT PROJECTS

300

HAPPY CLIENTS

650

COFFEES DRUNK

1568

FACEBOOK LIKES

STRATEGY & CREATIVITY

Phasellus iaculis dolor nec urna nullam. Vivamus mattis blandit porttitor nullam.

PORTFOLIO

We pride ourselves on bringing a fresh perspective and effective marketing to each project.

  • How to Fix "A Trust Relationship Between This Workstation and the Primary Domain Failed" in Windows

     

    A Trust Relationship Between This Workstation and the Primary Domain Failed

     

     

    How to Fix "A Trust Relationship Between This Workstation and the Primary Domain Failed" in Windows (Complete Guide)


    Fix Trust Relationship Failed Error in Windows Domain

    Learn how to fix "A Trust Relationship Between This Workstation and the Primary Domain Failed" using PowerShell, Active Directory, and domain rejoin methods.


    This issue prevents domain users from logging into their computers even though their passwords are correct.

    The good news is that the problem is usually not related to the user's password. Instead, it happens because the computer itself can no longer authenticate with the domain controller. 

     

    In this guide, you'll learn:


    • What this error means
    • Why it happens
    • What a secure channel is
    • How computer passwords work in Active Directory
    • Multiple ways to fix the problem
    • Best practices to prevent it in the future



    What Does This Error Mean?


    When a Windows computer is joined to an Active Directory domain, it creates a computer account inside Active Directory.

    That computer account behaves much like a user account.

    Instead of logging in with a username and password, the computer authenticates itself using a secret machine password, also called the computer account password.

    When Windows starts, the workstation silently proves its identity to the Domain Controller using this password.

    If both passwords match, authentication succeeds.

     

     

    If they don't match, Windows displays:

     

    A Trust Relationship Between This Workstation and the Primary Domain Failed


    Understanding the Computer's Local Secure Channel Password


    Many people confuse this with the user's Windows password.

    They are completely different.





    Think of it like this:

    Your user password identifies you.
    The computer password identifies the PC.

    The error occurs because the computer's identity cannot be verified anymore.




    What Is a Secure Channel?


    A Secure Channel is an encrypted communication path between a domain-joined computer and the Domain Controller.

    This secure channel allows Windows to perform tasks such as:

    • User authentication
    • Kerberos ticket requests
    • Group Policy processing
    • Password changes
    • Active Directory communication


    The secure channel depends on the machine account password.

    If that password becomes invalid, the secure channel breaks.



    How Windows Automatically Changes the Computer Password



    One interesting fact many administrators don't know is that Windows automatically changes the computer account password.

    By default:

    • Password changes every 30 days
    • Managed by the Netlogon service
    • The process happens automatically
    • Users never notice it


    The process is simple:

    1. Windows generates a new random machine password.
    2. The password is stored locally.
    3. The workstation contacts the Domain Controller.
    4. Active Directory updates the computer account password.
    5. Secure communication continues normally.


    No administrator intervention is required.


    Why Does the Trust Relationship Fail?


    There are several common reasons.


    1. Computer Stayed Offline Too Long


    This is the most common cause.

    Example:

    A company laptop is stored for several months.

    During this period:

    • Active Directory expects newer machine passwords.
    • The laptop still has the old password.
    • Authentication fails.


    Result:

    Trust relationship failed.


    Why Does the Trust Relationship Fail?


    There are several common reasons.


    1. Computer Stayed Offline Too Long

     
    This is the most common cause.

    Example:

    A company laptop is stored for several months.

    During this period:

    • Active Directory expects newer machine passwords.
    • The laptop still has the old password.
    • Authentication fails.


    Result:

    Trust relationship failed.


    4. Active Directory Restore


    Restoring a Domain Controller from an older backup may restore an outdated computer password.

    The workstation has the newer password.

    Again, the passwords no longer match.



    5. Virtual Machine Snapshot Rollback


    This is common in virtual environments.

    If a VM is reverted to an old snapshot, it also restores an old machine password.
    The Domain Controller still expects the newer one.



    Symptoms


    You may notice:

    • Trust relationship error during login
    • Unable to log in using domain credentials
    • Local Administrator login still works
    • Group Policy not updating
    • Authentication failures
    • Domain resources inaccessible
    • How to Verify the Problem


    Login using a local administrator account.

    Open PowerShell as Administrator and Run.

    • Test-ComputerSecureChannel



    Example output:

    • False


    This indicates the secure channel is broken.


     


    Method 1 – Repair Using PowerShell (Recommended)


    This is the fastest solution.

    Open PowerShell as Administrator.


    Run:

    • Test-ComputerSecureChannel -Repair -Credential DOMAIN\DomainAdminUser -Verbose



    Example:

    • Test-ComputerSecureChannel -Repair -Credential CONTOSO\AdminUser -Verbose


    If successful, PowerShell returns:

    • True



    Restart the computer.

    The trust relationship should now be restored.


    Method 2 – Reset the Computer Account


    On the Domain Controller:

    • Open Active Directory Users and Computers (ADUC).
    • Locate the computer object.
    • Right-click the computer.
    • Select Reset Account.
    • Confirm the action.


    Now return to the workstation.

    Repair the secure channel or rejoin the domain.



    Method 3 – Remove and Rejoin the Domain


    If PowerShell repair doesn't work:

    Open:
    System Properties


    Go to:
    Computer Name


    Click:
    Change


    Choose:
    Workgroup

    Restart the computer.


    Then:
    Join the domain again.
    Restart once more.


    This creates a new secure relationship with Active Directory.


    Method 4 – Using Netdom


    If RSAT tools are installed:

    • netdom resetpwd /server:DomainController /userd:Domain\AdminUser /passwordd:*


    Restart afterward.



    How the Authentication Process Works





    Best Practices to Prevent This Error



    Keep laptops online regularly
    Long periods without connecting to the domain can increase the chance of machine password synchronization issues.


    Avoid deleting computer accounts
    Delete computer accounts only when the device has been permanently decommissioned.


    Don't restore old VM snapshots
    Rolling back a virtual machine may also roll back its machine password.


    Verify before resetting
    Before resetting a computer account, confirm that the issue is actually related to the secure channel.


    Use PowerShell First
    Instead of immediately removing and rejoining the domain, try repairing the secure channel. It is faster and preserves the existing computer account relationship.



    Frequently Asked Questions (FAQ)


    Does this error mean the user's password is wrong?
    No.

    The user's password is usually correct. The issue is with the computer's secure channel password.


    Does Windows automatically change the computer password?
    Yes.

    By default, Windows changes the machine account password every 30 days using the Netlogon service.



    Conclusion


    The "A Trust Relationship Between This Workstation and the Primary Domain Failed" error is a common issue in Active Directory environments, but it is straightforward to resolve once you understand how machine account passwords and secure channels work.

    For most situations, repairing the secure channel with PowerShell is the quickest and least disruptive solution. If that doesn't work, resetting the computer account or rejoining the domain will restore communication with the Domain Controller.

    Whether you're an IT Support Engineer, System Administrator, or preparing for technical interviews, mastering this troubleshooting scenario is an essential skill that you'll likely encounter in enterprise Windows environments.



  • User Input Yes No Prompt in Batch Scripting

      

    User Input Yes No Prompt in Batch Scripting

     

    User Input Yes/No Prompt in Batch Scripting


    Many Batch Scripts require user confirmation before performing an action. For example, a script may ask whether you want to continue, delete a file, or restart a system.

    In such cases, we can use a **Yes/No Prompt** to collect user input and execute different actions based on the response.
    In this tutorial, we'll learn how to create a simple Yes/No prompt in Batch Scripting.



    Example Script

      
      
    @echo off
    
    :start
    cls
    set /p user_input=Do you want to continue? (yes/no)?:
    if not defined user_input goto start
    ::echo %user_input%
    if /i %user_input%==y goto Yes
    if /i %user_input%==n (goto No) else (goto InvalidInput)
    
    :: /i for upercase, lowercase, or mixed case letters. It makes the comparison case-insensitive.
    
    :Yes
    echo user has entered yes
    pause
    goto start
    
    :No
    echo user has entered no
    pause
    goto start
    
    :invalidInput
    echo %user_input% is an Invalid input. Please enter 'y' or 'n'.` 
    set user_input=
    pause
    goto start
      



    How the Script Works


    #Step 1: Ask for User Input

    set /p user_input=Do you want to continue? (yes/no)?:


    The user's response is stored in the variable:
    user_input


    Example:

    y
    OR
    n




    #Step 2: Check for Empty Input

    if not defined user_input goto start

    If the user presses Enter without typing anything, the script returns to the beginning and asks again.



    #Step 3: Handle "Yes"

    if /i %user_input%==y goto Yes

    If the user enters:
    y


    The script jumps to:
    :Yes



    Output:

    user has entered yes




    #Step 4: Handle "No"

    if /i %user_input%==n (goto No)
    If the user enters:
    n


    The script jumps to:
    :No


    Output:
    user has entered no



    #Step 5: Handle Invalid Input

    If the user enters something else:

    abc


    The script jumps to:
    :InvalidInput


    Output:


    abc is an Invalid input. Please enter 'y' or 'n'.
    The variable is cleared and the user is asked again.




    What Does /I Mean?




    The `/i` option makes comparisons case-insensitive.



    Example:



    The following inputs will all be treated as valid:
    y
    Y

    Without `/i`, Batch would treat uppercase and lowercase letters differently.



    Sample Output



    #User Enters Yes

    Do you want to continue? (yes/no)? y
    user has entered yes


    #User Enters No

    Do you want to continue? (yes/no)? n
    user has entered no


    #User Enters Invalid Value
    Do you want to continue? (yes/no)? hello

    hello is an Invalid input. Please enter 'y' or 'n'.





    Real-World Uses



    System Engineers often use Yes/No prompts for:

    • * Confirming software installations
    • * Starting backups
    • * Restarting services
    • * Deleting files or folders
    • * Running maintenance tasks
    • * Executing administrative scripts


    This helps prevent accidental actions.




    Interview Questions


    #What does `set /p` do?
    It takes input from the user and stores it in a variable.


    #What does `if not defined` do?
    It checks whether a variable is empty.


    #What does `/i` mean in an IF statement?
    It makes the comparison case-insensitive.


    #Why is input validation important?
    It prevents users from entering invalid values and improves script reliability.


    #What is the purpose of `goto`?
    It transfers execution to a specified label.



    Conclusion


    A Yes/No prompt is a simple but useful feature in Batch Scripting. It allows scripts to interact with users, validate input, and perform different actions based on the response. By combining `set /p`, `if`, and `goto`, you can build more interactive and user-friendly automation scripts.



  • ERRORLEVEL in Batch Scripting with Example

     

    ERRORLEVEL in Batch Scripting with Example

     

     

     ERRORLEVEL in Batch Scripting with Example


    When running commands in Batch Scripting, it is important to know whether a command was executed successfully or failed.



    This is where the **ERRORLEVEL** variable becomes useful.

    `ERRORLEVEL` helps us check the result of the previously executed command and take action based on success or failure.



    In this tutorial, we will learn how to use ERRORLEVEL with a practical example.

     What is ERRORLEVEL?


    `ERRORLEVEL` is a special system variable in Batch Scripting.


    After a command executes:

    * `0` usually means **Success**
    * Any non-zero value usually means **Error or Failure**



    Example


    text
    ERRORLEVEL = 0


    Means the command completed successfully.

    • ERRORLEVEL = 1



    Means an error occurred.



    Example: Checking a Folder Path


      @echo off
    
    set /P FolderPath=Enter the folder path:
    
     cd %FolderPath%
    
    if %ERRORLEVEL% EQU 0 (
          echo You entered the Correct path: %FolderPath% and it will exists
    ) else (
            echo You entered the Wrong path: %FolderPath%
    )
    
    
      

    How the Script Works


    Step 1

    The script asks the user to enter a folder path.

    set /P FolderPath=Enter the folder path:


    Example:

    C:\Users\user\Desktop


    Step 2

    The script attempts to change to the specified directory.


    cd %FolderPath%


    If the folder exists:

    ERRORLEVEL = 0



    If the folder does not exist:

    ERRORLEVEL = 1


    (or another non-zero value)


    Step 3

    The IF statement checks ERRORLEVEL.

    if %ERRORLEVEL% EQU 0

    If successful:

    • You entered the Correct path

    Otherwise:

    • You entered the Wrong path



    Example Output (Valid Path)

    • C:\Users\user\Desktop



    Output

    You entered the Correct path: C:\Users\user\Desktop and it will exists




    Example Output (Invalid Path)


    Input
    C:\InvalidFolder

    Output
    You entered the Wrong path: C:\InvalidFolder




    Why is ERRORLEVEL Important?



    ERRORLEVEL helps us:

    • * Detect command failures
    • * Validate user input
    • * Handle errors automatically
    • * Improve script reliability
    • * Create professional automation scripts


    Without ERRORLEVEL, a script may continue running even when something goes wrong.



    Real-World Uses for System Engineers


    System Engineers often use ERRORLEVEL for:

    • * Checking backup success
    • * Verifying software installation
    • * Network troubleshooting
    • * Validating file paths
    • * Monitoring scheduled tasks
    • * Automating maintenance scripts


    It is one of the most commonly used error-checking techniques in Batch Scripting.



    Interview Questions



    What is ERRORLEVEL in Batch Scripting?
    ERRORLEVEL is a system variable that stores the result of the last executed command.


    What does ERRORLEVEL 0 mean?
    It usually indicates that the command executed successfully.


    What does a non-zero ERRORLEVEL mean?
    It usually indicates an error or failure.


    Why is ERRORLEVEL used?
    It helps scripts detect and handle errors automatically.


    Can ERRORLEVEL be used with IF statements?
    Yes. It is commonly used with IF statements to check command results.



    Conclusion



    ERRORLEVEL is an essential feature in Batch Scripting that allows you to determine whether a command succeeded or failed. By combining ERRORLEVEL with IF ELSE statements, you can create smarter and more reliable scripts that handle errors effectively.

    As a future System Engineer or SecDevOps professional, understanding ERRORLEVEL will help you build automation scripts that are easier to troubleshoot and maintain.

  • FOR Loop in Batch Scripting with Examples

     

    FOR Loop in Batch Scripting with Examples


    FOR Loop in Batch Scripting with Examples


    When working with Batch Scripts, you may need to repeat the same task multiple times. Instead of writing the same command again and again, you can use a **FOR Loop**.

    The FOR Loop helps automate repetitive tasks such as displaying values, creating folders, processing files, and running commands multiple times.

    In this tutorial, we'll learn the basics of the FOR Loop using simple examples.



    What is a FOR Loop?


    A FOR Loop executes a block of code repeatedly for each item in a list.

    Syntax


    for %%variable in (list) do (
        command
    )


    Where:
     

    • `%%variable` stores the current value.
    • `list` contains the items to process.
    • `do` specifies the command to execute.



    Example 1: Display Numbers Using FOR Loop


    @echo off
    setlocal
    for %%i in (1 2 3 4 5) do (
        echo %%i
    )


    Output

    1
    2
    3
    4
    5



    Explanation


    In this example:

    • `%%i` acts as the loop variable.
    • The loop processes each value one by one.
    • The `echo` command displays the current value.



    Execution Flow:


    Iteration 1 → 1
    Iteration 2 → 2
    Iteration 3 → 3
    Iteration 4 → 4
    Iteration 5 → 5



    Example 2: Create Multiple Folders


    @echo off
    setlocal
    for %%i in (1 2 3 4 5) do (
        mkdir Folder-atul-%%i
    )

     

    Result


    The script creates the following folders:


    Folder-atul-1
    Folder-atul-2
    Folder-atul-3
    Folder-atul-4
    Folder-atul-5


     

    FOR Loop in Batch Scripting with Examples

     

     

     

    Explanation

    For each number:

    * The loop runs once.
    * `mkdir` creates a new folder.
    * `%%i` is appended to the folder name.

    This saves time compared to manually creating multiple folders.



    Why Use FOR Loops?


    FOR Loops are useful when:

    • * Creating multiple folders
    • * Renaming files
    • * Processing log files
    • * Running repetitive commands
    • * Automating administrative tasks




    Instead of writing:


    mkdir Folder1
    mkdir Folder2
    mkdir Folder3
    mkdir Folder4
    mkdir Folder5


    you can use a single FOR Loop.


    Real-World Uses for System Engineers

    System Engineers commonly use FOR Loops for:

    • Bulk folder creation
    • File management
    • Log collection
    • Backup automation
    • Software deployment tasks
    • User account processing



    FOR Loops are one of the most frequently used automation tools in Batch Scripting.





    Interview Questions


    What is a FOR Loop?
    A FOR Loop is used to repeat a command for each item in a list.


    What does %%i represent?
    It is the loop variable that stores the current value during each iteration.


    Why are FOR Loops useful?
    They reduce repetitive code and automate repetitive tasks.


    Which command is used to create folders?
    mkdir


    Can FOR Loops be used in automation scripts?
    Yes. They are widely used in system administration and automation tasks.



    Conclusion


    The FOR Loop is one of the most powerful features of Batch Scripting. It allows you to repeat tasks efficiently and automate routine operations. Whether you're displaying values, creating folders, or processing files, mastering FOR Loops will make your scripts cleaner and more effective.

    As a future System Engineer or SecDevOps professional, understanding loops is an important step toward automation and scripting mastery.
     

     

     

     

  • IF Statement and IF ELSE Statement in Batch Scripting

     

     

    IF Statement and IF ELSE Statement in Batch Scripting



    IF Statement and IF ELSE Statement in Batch Scripting



    When writing Batch Scripts, we often need to make decisions based on conditions. This is where the IF and IF ELSE statements are used.

    They allow a script to perform different actions depending on whether a condition is true or false.

    In this tutorial, we will learn how to use IF and IF ELSE statements with practical examples.

     

     

    What is an IF Statement?


    An IF statement executes a block of code only when a specific condition is true.

    Syntax

    if condition (
        command
    )



    Example 1: Basic IF Statement



      
    @echo off
    
    goto:main
    :main
    
    set /A number=5
    if %number% equ 5 (echo This is IF statement)
    
    goto:eof
    
      



    Output

    This is IF statement




    Explanation


    In this example:

    • A variable named number is created.
    • The value of number is set to 5.
    • The IF statement checks whether the value equals 5.
    • Since the condition is true, the message is displayed.



    The keyword:

    equ
    means equal to.


    Example 2: Checking a User Path



      
      @echo off
    
    goto:main
    
    :main
    
    set /P path= Enter path
    if %path% EQU C:\Users\user\Desktop\batch-scripting (
        cd %path%
        dir
    
    )
    
    goto:eof 
     
     
     
    IF Statement and IF ELSE Statement in Batch Scripting

     



    How It Works


    User enters a path.
    Batch compares the entered value with the specified path.

    If both match:

    • The script changes to that directory.
    • The contents of the folder are displayed using dir.



    Sample Input
    C:\Users\user\Desktop\batch-scripting


    Sample Output
    Directory of C:\Users\user\Desktop\batch-scripting


    This type of validation is commonly used in automation scripts.



    What is an IF ELSE Statement?


    The IF ELSE statement allows us to execute one block of code when a condition is true and another block when the condition is false.


    Syntax

    if condition (
        commands
    ) else (
        commands
    )



    Example: IF ELSE Statement


      
    @echo off
    goto:main
    :main
    set /P path= Enter path
    if %path% EQU C:\Users\user\Desktop\batch-scripting (
        cd %path%
        dir
    )
     else (
        echo Invalid path
    )
    goto:eof
    
      



    Explanation


    The script checks the path entered by the user.

    If the path is correct
    C:\Users\user\Desktop\batch-scripting


    The script:

    • Changes to the directory
    • Displays folder contents
    • If the path is incorrect
    • D:\Projects


    The script displays:

    Invalid path

    This makes scripts more user-friendly because they can handle both valid and invalid inputs.



    Common Comparison Operators


    IF Statement and IF ELSE Statement in Batch Scripting



    Example


    if %number% GTR 10 (
        echo Number is greater than 10
    )



    Real-World Uses of IF Statements


    System Engineers and IT Administrators commonly use IF statements for:

    • Checking whether a file exists
    • Validating folder paths
    • Verifying user input
    • Monitoring services
    • Automating troubleshooting tasks
    • Running backup scripts



    Interview Questions



    What is an IF statement in Batch Scripting?
    An IF statement executes commands only when a specified condition is true.


    What is the purpose of ELSE?
    ELSE executes an alternative block of code when the IF condition is false.


    What does EQU mean?
    EQU stands for "Equal To".


    Which command is used to take user input?
    set /P


    Why are IF statements important?
    They allow scripts to make decisions and automate tasks based on conditions.



    Conclusion


    The IF and IF ELSE statements are fundamental concepts in Batch Scripting. They help automate decision-making and make scripts more interactive. Whether you are validating user input, checking files, or automating system administration tasks, conditional statements are an essential skill for every Batch Scripting learner.




  • Local Scope vs Global Scope in Batch Scripting

     

    Local Scope vs Global Scope in Batch Scripting

     

     Understanding Local Scope and Global Scope in Batch Scripting



    Variables are one of the most important components of Batch Scripting. As scripts become larger, managing variables properly becomes essential. This is where the concepts of Local Scope and Global Scope come into play.

    In this article, we will understand how SETLOCAL and ENDLOCAL work and how they affect variable visibility inside a Batch Script.

    Learn Local Scope and Global Scope in Batch Scripting with practical examples. Understand SETLOCAL, ENDLOCAL, variable visibility, and best practices for Windows automation and System Administration.

    What is Variable Scope?


    Variable scope determines where a variable can be accessed within a script.

    In Batch Scripting there are two common scopes:

    • Global Scope
    • Local Scope


    Understanding the difference helps prevent unexpected behavior when writing automation scripts.

     

    Understanding SETLOCAL


    Example Script

      
      
    @echo off
    
    goto :displayname
    
    :displayname
    setlocal
    set /P name=Enter your name:
    echo Hello, %name%!
    endlocal
    
    goto: eof
      



    The command:

    setlocal

    creates a local environment.

    Any variable created after SETLOCAL exists only inside that local block.

    Example:

    setlocal
    set name=Atul

    The variable name is local.
    Once the script reaches:
    endlocal
    the variable disappears.




    Understanding ENDLOCAL

    @echo off
    
    goto :displayname
    
    :displayname
    setlocal
    set /P name=Enter your name:
    echo Hello, %name%!
    endlocal
    
    set /P number=Enter a number:
    echo You entered: %number%
    
    goto: eof
    
    


    The command:

    endlocal

    terminates the local environment and restores the previous environment.

    Example:

    • setlocal
    • set name=Atul
    • endlocal

    After ENDLOCAL, the variable:

    %name%

    is no longer available.





     


    Step-by-Step Execution of the Script



    Step 1

    Execution starts here:

    • goto :displayname
    • Control jumps to:
    • :displayname





    Step 2

    A local environment is created:

    • setlocal

    Now all variables created afterward are local.




    Step 3

    User enters a name:

    • set /P name=Enter your name:




    Example:
    Enter your name: Atul



    Variable value:
    name = Atul



    Step 4

    Display the value:
    echo Hello, %name%!


    Output:
    Hello, Atul!



    Step 5

    Local environment ends:
    endlocal

     

    Now the variable:
    name
    no longer exists.


    Step 6

    User enters another value:
    set /P number=Enter a number:


    Example:

    25

    Since this variable is declared outside the local block, it remains available.

    Output:
    You entered: 25


    What is Global Scope?


    A global variable is accessible throughout the entire script.


    Example:

      
    @echo off
    
    set company=HackingTruth
    
    echo %company%
    
    goto:eof
      


    Output:

    HackingTruth


    The variable remains available until:

    • Script ends
    • Variable is modified
    • Variable is deleted



    Example of Local Scope


      @echo off
    setlocal
    
    set username=Atul
    echo %username%
    
    endlocal
    echo %username%
      


    Output:
    Atul

    Second output:

    (blank)

    Because username exists only inside the local environment.



    Example of Global Scope

    @echo off
    
    set username=Atul
    
    echo %username%
    
    echo %username%
    



    Output:

    Atul
    Atul

    The variable is available everywhere in the script.


    Why Use Local Scope?


    Local variables are useful when:

    • Creating reusable functions
    • Preventing accidental variable modification
    • Testing code
    • Building large automation scripts


    Benefits:

    • Better script organization
    • Reduced bugs
    • Cleaner variable management

     

     

     

    Real-World Example


    Suppose a System Engineer writes a backup script.

    Without local scope:

    • set backup=C:\Backup
    • Another function may accidentally overwrite:
    • set backup=D:\Temp




    Result:

    • Backup process fails
    • Wrong directory used


    Using SETLOCAL prevents such conflicts.




    Preserving a Variable After ENDLOCAL


    Sometimes you want to keep a variable after ENDLOCAL.

    Example:

      @echo off
    setlocal
    set username=Atul
    endlocal & set username=%username%
    echo %username%
      



    Output:

    Atul

    This technique is commonly used in advanced Batch scripts.




    Local Scope vs Global Scope

     

    Local Scope vs Global Scope in Batch Scripting



    Real-World Uses for System Engineers



    Local Scope is useful for:

    • Software deployment scripts
    • Backup automation
    • User provisioning scripts
    • Active Directory automation
    • Network troubleshooting tools
    • System inventory collection




    Global Scope is useful for:

    • Configuration values
    • Common paths
    • Server names
    • Shared settings




    Interview Questions



    What is SETLOCAL in Batch Scripting?

    • SETLOCAL creates a local environment where variables exist only within that block.


    What is ENDLOCAL?

    • ENDLOCAL ends the local environment and removes local variables.


    What is the difference between Local Scope and Global Scope?

    • Local variables are available only inside a SETLOCAL block, while global variables remain available throughout the script.


    Why should System Engineers use SETLOCAL?

    • It prevents variables from interfering with other parts of the script and improves maintainability.


    What happens to variables after ENDLOCAL?

    • They are removed unless specifically preserved.



    Conclusion


    Understanding Local Scope and Global Scope is essential for writing professional Batch Scripts. Using SETLOCAL and ENDLOCAL allows you to create safer and more maintainable automation scripts. As your scripts grow in complexity, proper scope management becomes increasingly important.

    For aspiring System Engineers and future SecDevOps professionals, mastering variable scope is a foundational skill that helps build reliable Windows automation solutions.





  • Batch Scripting Functions Explained: CALL, GOTO, EXIT and EXIT /B with Examples

     

     

    Understanding Functions in Batch Scripting: CALL, GOTO, EXIT, and EXIT /B

     

     

    Understanding Functions in Batch Scripting: CALL, GOTO, EXIT, and EXIT /B


    As batch scripts become larger, writing all commands in a single block can make the script difficult to maintain. Functions help organize code into reusable sections, making scripts easier to read and manage.

    In this article, we will learn how functions work in Batch Scripting using labels, the CALL command, GOTO, EXIT, and EXIT /B.

    Learn Batch Scripting functions with practical examples. Understand CALL, GOTO, GOTO :EOF, EXIT, and EXIT /B to build reusable Windows automation scripts for System Administration and System Engineering.

     

    What is a Function in Batch Scripting?


    Unlike programming languages such as Python or Java, Batch scripting does not have traditional functions. Instead, functions are simulated using labels.

    A label begins with a colon (:).


    Example:

    :function1
    
    You can jump to a label using:
    
    goto:function1
    
    or call it using:
    
    call:function1
      

     

    Basic Function Example

      
      
    @echo off 
    :function1
    
    echo This is function 1
    
    :function2
    
    echo This is function 2
    
    call:function1
      

    Explanation


    In this script:

    Two labels are created.
    function1 prints a message.
    function2 prints another message.
    call:function1 invokes function1.



    Output

    This is function 1
    This is function 2
    This is function 1



    Important Note

    Without goto statements, Batch continues reading the script from top to bottom, executing labels as it encounters them. 

     

    Using GOTO to Control Execution



    The goto command transfers execution directly to a specific label.

    Example:

      
      @echo off
    
    goto:function3
    
    :function1
        echo This is function 1
    
    :function2
        echo This is function 2
        goto:eof
    
    :function3
        echo This is function 3
        call:function1
      




    How It Works


    Step 1

    Script starts at:

    • goto:function3
    • Execution jumps directly to:
    • :function3




    Step 2

    Output:

    • This is function 3



    Step 3

    • call:function1 executes:
    • This is function 1


    Output


    This is function 3
    This is function 1

    Notice that function2 is skipped because execution never reaches it.




    Understanding GOTO


    One of the most useful statements in Batch scripting is:

    • goto:eof


    EOF means End Of File.



    When a called function reaches:

    • goto:eof


    execution returns to the line immediately after the CALL statement.



    Example:



    • :function2
    • echo This is function 2
    • goto:eof


    This behaves similarly to a return statement in other programming languages.

    Function Example with EXIT



    Example:

      
      
    @echo off
    
    goto:function3
    
    :function1
        echo This is function 1
    
    :function2
        echo This is function 2
        timeout 5
    exit
    
    :function3
        echo This is function 3
        call:function1
    
      


    Explanation

    Here:

    • exit


    terminates the entire Command Prompt session.

    If this script reaches the exit command:

    • CMD window closes.
    • Remaining commands do not execute.



    Why Use EXIT?


    Useful for:

    Ending automation tasks
    Stopping deployment scripts
    Exiting maintenance operations

    However, it should be used carefully because it closes the current command interpreter.



    Understanding EXIT /B


    A safer alternative is:

    exit /B

    Example:

      
      @echo off
    
    goto:function3
    
    :function1
        echo This is function 1
    
    :function2
        echo This is function 2
        exit /B 0
    
    :function3
        echo This is function 3
        call:function1
    
    
      


    What Does EXIT /B Do?


    Instead of terminating CMD:

    • exit /B

    only exits the current function or batch script context.



    The command:

    • exit /B 0


    returns an exit code of 0.



    Meaning of Exit Codes


    Batch Scripting Functions Explained: CALL, GOTO, EXIT and EXIT /B with Examples

     

     

     CALL vs GOTO



    Many beginners confuse these commands.

    • CALL
    • call:function1
    • Executes the function
    • Returns to the next line after completion



    Example:


    • Start
    • Function1
    • Back to Start
    • GOTO
    • goto:function1
    • Jumps permanently
    • Does not return automatically




    Example:

    • Start
    • Function1


    Execution continues from the new location.




    Real-World Uses for Functions



    Functions are useful when:


    • Displaying menus
    • Automating backups
    • Creating reusable code blocks
    • Network troubleshooting scripts
    • Software deployment scripts
    • User management automation
    • System information collection



    Instead of writing the same commands repeatedly, create one function and call it whenever needed.



    Practical Example: System Information Function

     

    @echo off
    
    call:sysinfo
    goto:eof
    
    :sysinfo
    echo Hostname:
    hostname
    
    echo.
    echo Current User:
    whoami
    
    echo.
    echo IP Configuration:
    ipconfig
    
    goto:eof
    

     

     


    Benefits:


    • Cleaner code
    • Easier maintenance
    • Reusable logic


    Interview Questions


    What is a function in Batch Scripting?

    A function is a labeled section of code that can be executed using the CALL command.



    What is the difference between CALL and GOTO?

    CALL returns to the original location after execution, while GOTO permanently jumps to another label.



    What does GOTO do?

    It exits the current function and returns control to the calling statement.



    What is EXIT /B?

    It exits the current batch context without closing the entire Command Prompt window.



    Why are functions useful?

    Functions improve code reusability, readability, and maintainability.



    Conclusion



    Functions are one of the most important concepts in Batch scripting. By understanding labels, CALL, GOTO, GOTO :EOF, EXIT, and EXIT /B, you can write structured and reusable scripts rather than long, repetitive command files.

    For System Engineers and future SecDevOps professionals, mastering functions is essential because real-world automation scripts often depend on reusable code blocks and proper flow control.


  • WHAT WE DO

    We've been developing corporate tailored services for clients for 30 years.

    CONTACT US

    For enquiries you can contact us in several different ways. Contact details are below.

    Hacking Truth.in

    • Street :Road Street 00
    • Person :Person
    • Phone :+045 123 755 755
    • Country :POLAND
    • Email :contact@heaven.com

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.