Wednesday, November 3, 2010

Single line Commands on Windows

Check if a domain user exists. Add user if it does not exist.
  • Net localgroup Administrators  |  findstr /i UserName1  > NULIF %ERRORLEVEL%  NEQ 0  ( net localgroup Administrators UserName1 /ADD )
Check if a share exists. Create share if it does not exist.
  • Net Share | findstr  /i  %SystemDrive%\Temp  |  findstr /i my > NUL  &  IF %ERRORLEVEL%  NEQ 0  ( net share My=%SystemDrive%\Temp /GRANT:Users,FULL )
Change File permissions inside a folder
  • Applicable vista onwards.
  • DIR C:\Temp\WinTest-*.log 2> NUL 1> NUL & IF %ERRORLEVEL% EQU 0 FOR %I in (C:\Temp\WinTest-*.log) do ICACLS %I /grant EveryOne:(F)

There will definitely be Powershell, WMI and Vbscript way of doing same thing, but sometimes we do need CMD commands.

No comments: