how to delete specific user remotely - windows

I need to delete specific user example "UserExample" from around 400 windows PCs remotely so I made this batch file but this will delete local PC user not the remote any one can help me to improve that batch
note there is no domain server linked to these pcs
thanks
#echo On
Title %1 - DeleteUser from
:NETUSE
Net use "\\%1\c$" 123456 /user:admin
if %errorlevel% NEQ 0 goto :NETUSE
net user UserExample /delete
pause
EXIT

I would assume you could use WMIC to connect to all the units listed in a text file and delete the user from them.
Something along the lines of this, perhaps:
WMIC /Output:UserDelete.log /Node:#PCList.txt /User:Administrator /Password:Pa55w0rd UserAccount Where "Name='UserExample' And LocalAccount='TRUE'" Delete
I do find it hard to believe that a single user has created accounts on 400 PC's though

Related

Code to automatically remove users that have numbers from 1-500 at the end

I recently ran a program that automatically generates 500 users on a virtual machine, and it is making it lag on the lockscreen. Basically, it generates 500 users in the format of: "userX (user1, user2, user3 etc.). I was wondering if there could be a batch script / powershell script / just anything that could be done to remove all of these users. The process can be done manually using net user userX /del but it could probably be automated using the same command again but with numbers counting up. Thanks.
for /L %a in (1,1,500) do #net user user%a /del
Or if in a batch file:
for /L %%a in (1,1,500) do #net user user%%a /del

Trying to Copy Data from a Specified User's Network Drive but Getting an Error

I am trying to use a script to restore a backed-up folder that was created before upgrading a user's computer to windows 10.
The script asks for the subfolder that the user's home folder is located under and the user's username. Once it has both of those, it inputs the values of those variables into a network path, and then maps that network path to the H drive of the admin account used to open CMD.
However, after entering the users credentials when the script attempts to access the specified folder, I get this error:
"System error 1312 has occurred. A specified logon session does not exist. It may already have been terminated."
Here is my script:
#echo off
:main
set /p location=Please enter the location of the user (These can be located at \\<domain>\root\HOMES):
set /p usern=Please enter the username:
net use H: \\<domain>\root\HOMES\%location%\%usern%\win10backup
echo Before proceeding, please confirm that the backup folder is named "win10backup" (without quotes)
set /p input=Enter 'y' to continue:
xcopy H:\win10backup C:\Users\%username%\ /O /X /E /H /K
Note: is my companies domain. Removed for security reasons.
Any help would be greatly appreciated.

Bat file to remove all user folders with 20 in the name bat file

I just started working in a school and the way we have the computers set up they automatically download a students user from the network when a student logs into any computer. We're working on making a script to run this command for cleaning them :
for /D %f in (*20**) do rmdir %f /s /q
In command prompt it shows every user with 20 in the name and deletes them from the computer along with everything in these folders.
We can't just change the file name to .bat instead of storing it in a note pad to copy to a command prompt window.
The batch file we currently have is
#echo off
pause
for /d %%f in (*20**) do rmdir C:\Users\%%f /s /q
pause
Which shows each pause to try to help me debug it but doesn't delete the folder. What am I doing wrong here?
I wouldn't do this. As I assume it's something like:
Regular%20User
%20 is basically a space.
To address your issue of wanting to clean them, as I assume it is the temp profile that is created when they login(from a network profile), rather than making the script clean anything with 20 in it, make a script that removes users locally that are of a certain Group Policy group. That way your admin accounts stay locally, and if your script is done right, your student's profiles are autocleaned after a set delay. (I would recommend 30 days, as you can use the temp profile as evidence if a student is caught on a computer he/she shouldn't be on :)

Read variable from external file not working on running as scheduled task

I would like to run a batch file after resuming from sleep state in Windows.
If I start the batch file on command line everything works as expected.
But the batch script does not run properly as scheduled task.
What I have done:
External config file AutoMountConf.bat contains set Pass = Test
Local script file scheduleTask.bat contains
rem AutoMountConf.bat is in my intranet.
call X:\AutoMountConf.bat
start "" "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q
On command line the TrueCrypt container is mounted.
If I run the script from scheduled task I get the login screen to type the password manually.
There are two or perhaps even three issues.
The first one is set Pass = Test instead of set "Pass=Test" as Stephan reported already. For more details on how to assign a value right to an environment variable see my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?
The second issue is caused by the fact that network drives once mapped by a user to a drive letter and remembered in registry by Windows are automatically disconnected by Windows on user logs off and are only reconnected if the same user logs on again.
For a scheduled task it is therefore very often necessary to use UNC paths for files and folders on a shared folder in network or connect the network drive and disconnect it in the batch file itself executed as scheduled task.
It is not possible to call a batch file with UNC path. Windows does not allow that. Therefore it is necessary to connect and disconnect to network share manually in the batch file. I offer 2 solutions for this problem.
The first one is using command net use:
%SystemRoot%\System32\net.exe use X: \\ComputerName\ShareName password /user:Domain\UserName /persistent:no
if not errorlevel 1 (
call X:\AutoMountConf.bat
%SystemRoot%\System32\net.exe use X: /delete
start "" /wait "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q
)
password and /user:Domain\UserName is necessary only if the scheduled task is not executed with a user account which has the permissions to access the batch file on the remote machine. In general it is much more secure to define the scheduled task with the right user account and safe the password also for this account together with the task. Windows stores the password for the task encrypted like it does it also for the user account itself.
Run in a command prompt windows net use /? for details on the required and optional options. /persistent:no is what avoids remembering the network share in Windows registry for automatic reconnect after login by same user.
The second one is using commands pushd and popd:
pushd \\ComputerName\ShareName
if not errorlevel 1 (
call AutoMountConf.bat
popd
start "" /wait "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q
)
Please execute in a command prompt window pushd /? and read the output help to understand why this works.
But this solution requires that the user account used for the scheduled task with correct password is one which has appropriate permissions on the share on the remote computer. Password and user name can't be specified with this solution in the batch file itself.
if not errorlevel 1 means if previous command exited NOT with a value greater or equal 1 meaning if exit code of previous command is 0 and therefore command execution was successful. It can always happen that the remote machine is currently not available on network and therefore it is always good to check success on connecting to share on remote machine.
There is perhaps one more reason why Pass is not defined after running AutoMountConf.bat.
AutoMountConf.bat contains setlocal and the variable Pass is defined after this command was executed and before endlocal is executed in same batch file or implicitly called by command processor on exiting AutoMountConf.bat.
setlocal results in creating always a copy of existing environment variables and all modifications on environment variables are done on this local copy. The previous environment variables are restored on execution of (matching) endlocal or when end of a batch file is reached in which case the command processor automatically restores previous environment.
Please execute in a command prompt window setlocal /? and read the output help.
For examples to understand environment management by commands setlocal and endlocal perhaps even better see answers on Echoing a URL in Batch and Why is my cd %myVar% being ignored?
set Pass = Test
sets a variable pass<space> with the Content <space>Test. So %pass% keeps empty.
use this Syntax:
set "Pass=Test"
to avoid any unintended spaces.

Batch File: Need conditional script that would log out any user apart for me

I am looking for some help.
I am looking to create a local logon script on Local GPO on my desktop.
What I need to do is, when any user logs in, this script will fire and check the currently logged in user, and if it's not my account then it would logoff.
Something like:
IF NOT %username% = Domain\me shutdown.exe -l
Any other solutions also accepted.
what's you are looking is HELP IF
The syntax should look like:
#echo off
rem add your username here after the = without space.
set "_MyUsername=MyUsername"
if [%username%] neq [%_MyUsername%] shutdown.exe /l /f || exit /b 0

Resources