If Computer Name Contains

powershell if computername contains
run batch file based on computer name
run script based on computer name
batch file computer name variable
if %computername%==
powershell if hostname equals
computername 0 2
if not exist batch

I need to create a batch file that moves computer name object in Active Directory only if computer name contains some string, like:

If %computername% contains "LAP" 
( dsmove "CN=%computername%,CN=Computers,DC=domain,DC=local" -newparent"OU=**Laptops**,OU=Computers,OU=Company,DC=domain,DC=local" )

    If %computername% contains "DESK" 
        (dsmove "CN=%computername%,CN=Computers,DC=domain,DC=local" -newparent "OU=**Desktops**,OU=Computers,OU=Company,DC=domain,DC=local" )

What is the correct command please?

set check_computername=%computername:LAP=%
if "%check_computername%" EQU "%computername%" (
  echo computer name contains "LAP"
) else (
  echo computer name does not contain "LAP"

You can put your stuff in if and else blocks.

Case Insensitive solution:

echo %check_computername%| Find /I "LAP" >nul 2>&1 || echo does not contain LAP
echo %check_computername%| Find /I "LAP" >nul 2>&1 && echo does not contain LAP

how do i write an IF statement that will identify a specific character in , IF %computername% contains?? "-6-" goto MOD6. :MOD6 if ($computer - match "mod") {#do something here}. Thursday, August 12, 2010� If you are upgrading a computer whose NetBIOS name contains a period, change the machine name. For more information, see the "Special characters" section. In Windows 2000 and in later versions of Windows, computers that are members of an Active Directory domain cannot have names that are composed completely of numbers.

IF NOTERRORLEVEL 1 ( dsmove ... OU=laptop ... )
IF NOTERRORLEVEL 1 ( dsmove ... OU=desktop... )

Batch File Login Script Help - IF COMPUTERNAME, drive mappings and other functions in the login script and go straight to ":END". Would this work? @ECHO OFF IF %COMPUTERNAME%==SV-� In AD if you try to search for computer name and would like to be slightly more specific you have a problem. Here is the default choice: The rather obvious choice would include “computer name contains” but sadly the option is not there! Solution? Simple – use LDAP search string instead! In ADUC define new query with custom search for:

The logic has to be reversed. Here's a case-insensitive solution:

setlocal enabledelayedexpansion    
set nameSearch=Lap
set checkComputerName=!computername:%nameSearch%=!
if "%checkComputerName%" NEQ "%computername%" (
  echo %nameSearch% found in %computername%
) else (
  echo %nameSearch% not found in %computername%

If Hostname contains run command : PowerShell, I need a script to check if Hostname contains/matches … All locations have an abbreviation at the beginning of their names. (now the powershell window is acting as part of the linux computer at work) type "jupyter notebook --port 1234". Computer Name change is being blocked due to nonexistent disallowed key characters I recently had repairs done on my laptop, including having a new hard drive installed. After it was fixed, I discovered that the computer name had been changed, so I decided to change it back again.

Came across this issue today, this is how I solved it..

Say you have different names for desktops, laptops etc ie (DESKTOP0001, LAPTOP0001) etc, then this method will work nicely.

What you want to do is grab the first few characters of the name, you can use :x,y within a variable to do this.


echo %compuername:~0,6%

The output for this would be DESKTO (first 6 characters starting from position 0)

(echo %computername:~1,6% would give you ESKTOP )

Quick proof test..

if %computername:~0,6% == DESKTO echo yes-Desktop

So for my use, I used

if %computername:~0,6% == DESKTO goto Desktop
if %computername:~0,6% == LAPTOP goto Laptop

goto end

enter Desktop commands here
goto end

enter Laptop commands here
goto end


Detecting computer name, How can I detect if I am on a certain host computer in a batch script? Ex if I run the command hostname it gives me the current computers hostname. Is there a way� The array of computer names is created by using the Get-Content cmdlet to read a text file that contains a list of computer names. The text file, servers.txt, is a plain ASCII text file that has a list of computer names on individual lines, as shown in the following image:

[SOLVED] How can I use "hostname" in a batch script?, Solution: IF %computername%==MYCOMPUTER net use Y: conveniently, % computername% is a variable containing the exact same information as running � My computer name contains "LAP" in it. I tried to run this script and it says computer name does not contain "LAP". – Nick Jun 25 '13 at 9:18.

Would like to only run this script if computer name is a certain name , HiMy script works great, now I would like to add only run this script if the computer name is equal to test, my if statement doesnt seem to work,� 2 Questions. 1. Is there a faster way to do this. 2. How can I get this sorted is it as simple as piping it to a sort command. later in the script is goes thru each pc and my OCD cannot permit the computer names showing up in random order

Batch file match start of machine name, SET Name=%computername% SET FirstThreeChars=%computername:~0,3%. Then run through your IF loop using the FirstThreeChars�

  • there is a logical error: change EQU to NEQ or adapt the ECHOs
  • No. My computer name contains "LAP" in it. I tried to run this script and it says computer name does not contain "LAP".
  • @npocmaka solution should work beware that the substitution is case-sensitive !
  • So how to do it incase-sensitive?
  • Convert computer name to uppercase before compareison see robvanderwoude.com/battech_convertcase.php
  • You solved the problem string starts with, but the question was about string contains