I recently attended a Windows Server 2003 training seminar. During our class discussion, the instructor mentioned several operations, a few of which were scriptable. Out of the approximately 20 students in the room, all of whom were experienced systems administrators, only a few of us had any scripting experience. The others wanted to hear more after they learned about the possibilities. As a scripting evangelist, I often discover that once systems administrators get bitten by the scripting bug, they're hooked for life. Scripts let you accurately and quickly perform repetitive functions. So, for example, knowing how to write a script to capture information from several remote nodes and create meaningful reports can make the task fun instead of drudgery.

For many systems administrators, the first question is, How do I get started? For the beginner, the difference between scripting 1 line of code to display the words "Hello World" on screen and scripting 200 lines of code to perform a meaningful task is a quantum leap. If you're new to scripting, you probably just want to know what scripting can do for you. Often, the best approach is to start by running some simple but useful commands. Then, after you master the basics, you can turn to more advanced resources, including books about scripting, to enhance your skills.

I've created 101 "how to" scripting commands to answer 101 of the most common "How can I?" questions, and I provide some sample script code to get you started. These examples are designed to introduce you to the power of shell scripting, get you hooked on the potential of scripting for making your job easier, and provide you with greater safety, security, and accuracy when performing your daily tasks.

Getting the Scripting Commands to Work in Your Environment
Before you jump in, you need to ensure that the scripting commands presented in this article will work in your environment. Be aware that I tested these commands on Windows XP Professional Edition Service Pack 1 (SP1) and Windows 2000 SP3. I've provided the following prerequisites and tips to help you get started. Let's first consider the following prerequisites:

  • Install the utilities that the code requires. For a list of these resources, see Web Table 1,, InstantDoc ID 41198, or refer to the path name hints I've used that apply to my test machine. Most of the tools are from the Microsoft Windows 2000 Resource Kit and Win2K Support Tools or are built-in commands, but a few are third-party freeware tools.
  • Configure the code to include the path to the utilities you installed.
  • Configure the Lightweight Directory Access Protocol (LDAP) path or other required information if the script doesn't prompt you for user input.
  • Next, let's consider a few tips that can make running the scripts easier:

  • Many of the scripts use the Set /P command to ask the user for input. If you'd rather use hard-coded server names or usernames, remove "(SET /P C=Enter computer name: ) &" and replace the !C! or !U! variable with the hard-coded computer name, username, or other information for which the script prompts you.
  • You can create a shortcut that in turn creates an environment in which to run the scripts. Typically, when you add code to a script, you set a variable, such as a computer name, in one line of code and use that variable in another line of code later in the script, such as you see in this example:
(SET /P C=Enter computer name: )

However, because we're going to create one-line scripts, you need a way to set the variable in one line. If you simply use the Set /P command on the same code line as the variable, you must operate in a command-shell environment in which the Setlocal command has been run with the optional argument Enabledelayedexpansion.

Instead of opening a command prompt and running the Setlocal Enabledelayedexpansion command every time you want to run a script, you can run the following one-line command to create a OneLinerCmd shortcut on your desktop:

C:\ResourceKit\Reg.exe add
  HKCU\Console\onelinercmd  ScreenBufferSize=131072080
  REG_DWORD & shortcut.exe -f
  -t "%systemroot%\system32\cmd
  .exe" -a "/V:ON" -d %system
  drive%\ -n "%userprofile%  desktop\OneLiner Cmd"

(Although this command and the other one-line commands in this article wrap to multiple lines, lines of code that you cut and paste from this article might not work properly until you "unwrap" them to one line. You can download the code in the form of a properly formatted text file from the Windows Scripting Solutions Web site.) The code uses the Cmd /V:ON command to launch a script environment that achieves the same result as using Setlocal. Note that the shortcut that the script creates will change the screen buffer size to give you a larger scrolling window for your command output.

  • Copy and paste the scripts into a command-shell window. Highlight the code you want to run, then right-click in the command window to paste in the code. If you can't paste in code, open the Command Prompt Properties window, select the Options tab, and ensure that the QuickEdit and Insert modes are enabled by selecting the associated check boxes.
  • The commands and tools included in the scripts in this article offer many more options than I can describe. If you encounter differences running these commands in Windows 2003 or if you just want to see all the options that might be useful in your environment, type
<i>Command Name /?</i>

Also, multiple versions might exist for some tools, such as reg.exe and gpresult.exe. Check to be sure that the version of the utility you're using supports the syntax presented in this article.

  • Several of the scripts that run .vbs files use CScript to run those files. When you call a .vbs file with CScript, you must provide the full path to the .vbs file.
  • Carefully test the scripts in a development environment before you use them in production. Notice that the scripts in this article are primarily data acquisition scripts and, with a few exceptions of some temporary file-cleanup scripts, the scripts perform no changes to your files or groups.
  • All scripts are limited to the authority of the logged-on user who's executing them. You won't be able to run most of the scripts without Administrator group membership on local or remote servers or clients.
  • As the demand for one-to-one correspondence between GUI and scripted operations has grown, Microsoft has added scripting tools to the OS. In some cases, these tools are new (e.g., dsquery.exe), and in other cases, these tools came from the Win2K resource kit, Microsoft Windows NT Server 4.0 Resource Kit, or Win2K Support Tools (e.g., gpresult.exe). If you plan to run these scripts in Windows 2003, check to be sure that the tool isn't already installed before you add any resource kit tools.

AD Scripting Commands
Active Directory (AD) can provide a wealth of information. Unfortunately, the Microsoft Management Console (MMC) Active Directory Users and Computers snap-in allows only simple list export functions that limit the types of information you can retrieve. However, you can use several resource kit utilities in the AD scripting commands below to mine the wealth of information available in AD.

1. How can I export a list containing all Sales domain computers?

Csvde.exe -f C:\complist.csv -r
  "(objectClass=computer)" -d
  com" -p subtree

2. How can I search for the LDAP path of a computer in my domain?

(SET /P C=Enter computer name:
  ) &amp For /f "tokens=1,2,3,*"
  %i in ('CScript //nologo
  company,dc=com" /Q /S:subtree
  (cn=!C!)"') do ECHO %l

3. How can I see the detailed Resultant Set of Policies (RSoP)?

(SET /P C=Enter computer name:
  ) &amp gpresult.exe /S !C!

4. How can I find all XP computers in my organizational unit (OU) that have SP1 installed?

For /f "tokens=1,2,3,4" %i in
  ('CScript " //nologo
  company,dc=com" /Q /C:"&amp
  (operatingsystem=Windows XP
  pack 1)"^| find " objects."')
  do ECHO %j

5. How can I get a list of all computers in an OU, including any sub-OUs?

For /f "tokens=3,* delims<h1><a name="_i_in_CScript_nologo_C_supporttools_search_vbs_LDAP_dc_sales_dc_your_company_dc_com_S_subtree_Q_C_amp_amp_ObjectCategory_computer_do_ECHO_i_j_gt_gt_D_PCs_csv_pre_p_font_class_subhead_6_How_can_I_determine_which_computer_accounts_are_disabled_in_my_OU_font_br_p_pre_For_f_tokens_i_in_C_w2k3tools_dsquery_exe_computer_LDAP_dc_sales_dc_yourcompany_dc_com_disabled_do_ECHO_i_pre_p_font_class_subhead_7_How_can_I_find_all_disabled_user_accounts_in_my_OU_font_br_p_pre_For_f_tokens_i_in_C_w2k3tools_dsquery_exe_user_LDAP_dc_sales_dc_your_company_dc_com_disabled_do_ECHO_i_pre_p_font_class_subhead_Computer_Management_Scripting_Commands_font_br_Many_of_the_computer_management_code_examples_below_let_you_remotely_query_and_configure_systems_and_eliminate_the_necessity_of_physically_logging_on_to_every_client_Automating_your_day_to_day_computer_management_tasks_can_be_a_major_time_saver_p_p_font_class_subhead_8_How_can_I_determine_whether_a_remote_computer_is_on_with_Ping_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_For_f_i_in_Ping_n_1_C_Find_Reply_do_ECHO_Online_ECHO_Offline_pre_p_font_class_subhead_9_How_can_I_determine_whether_a_remote_computer_is_on_with_NBTstat_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_For_f_i_in_NBTstat_a_C_Find_found_do_ECHO_Offline_ECHO_Online_pre_p_font_class_subhead_10_How_can_I_determine_whether_a_remote_computer_is_on_with_Net_Use_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_For_f_tokens_i_in_Net_Use_C_2_gt_NUL_find_successfully_do_ECHO_Online_ECHO_Offline_pre_p_font_class_subhead_11_How_can_I_compare_the_time_on_the_local_computer_with_the_logon_server_font_br_p_pre_For_f_tokens_i_in_Net_Time_COMPUTERNAME_find_V_command_do_ECHO_i_amp_ampFor_f_tokens_i_in_Net_Time_LOGONSERVER_find_V_I_command_do_ECHO_i_pre_p_font_class_subhead_12_How_can_I_compare_the_time_on_a_remote_computer_with_the_logon_server_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_For_f_tokens_i_in_Net_Time_C_find_V_command_do_ECHO_i_amp_ampFor_f_tokens_i_in_Net_Time_LOGONSERVER_find_V_command_do_ECHO_i_pre_p_font_class_subhead_13_How_can_I_find_all_the_computers_in_my_Win2K_domain_that_have_the_word_Sales_in_the_computer_name_font_br_p_pre_For_f_tokens_1_delims_i_in_C_resourcekit_global_exe_Domain_Computers_LOGONSERVER_Find_Find_i_SALES_do_ECHO_i_pre_p_font_class_subhead_14_How_can_I_find_all_the_computers_in_my_NT_4_0_domain_that_have_the_word_Sales_in_the_computer_name_font_br_p_pre_For_f_tokens_1_delims_i_in_C_resourcekit_global_exe_Domain_Users_LOGON_SERVER_Find_Find_i_SALES_do_ECHO_i_pre_p_font_class_subhead_15_How_can_I_create_a_shortcut_on_my_desktop_to_manage_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_for_shortcut_amp_amp_shortcut_exe_f_t_SystemRoot_system32_compmgmt_msc_a_computer_C_d_System_Root_system32_n_USER_PROFILE_desktop_C_CompMgmt_pre_p_font_class_subhead_16_How_can_I_check_the_trust_relationship_between_a_computer_and_its_domain_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_C_pstools_psexec_exe_c_C_C_supporttools_netdiag_exe_test_trust_pre_p_font_class_subhead_17_How_can_I_get_system_information_about_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_C_pstools_psinfo_exe_C_pre_p_font_class_subhead_18_How_can_I_see_details_of_all_the_running_processes_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_C_pstools_pslist_exe_C_pre_p_font_class_subhead_19_How_can_I_see_the_last_five_events_in_the_System_log_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_C_pstools_psloglist_exe_n_5_system_C_pre_p_font_class_subhead_20_How_can_I_determine_whether_the_McAfee_virus_definitions_have_been_updated_in_the_past_7_days_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_C_pstools_psloglist_exe_o_mclogevent_application_d_7_C_pre_p_font_class_subhead_21_How_can_I_see_the_past_10_logon_events_in_the_System_log_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_C_pstools_psloglist_exe_i_6009_n_10_C_pre_p_font_class_subhead_22_How_can_I_see_a_list_of_computer_accounts_that_have_password_ages_more_than_180_days_old_font_br_p_pre_C_systools_netpwage_exe_Machines_v_Min_180_Tabs_pre_p_font_class_subhead_23_How_can_I_shut_down_and_restart_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_C_resourcekit_shutdown_exe_C_R_C_pre_p_font_class_subhead_24_How_can_I_change_the_username_in_the_logon_box_back_to_the_original_username_after_I_log_on_to_a_computer_and_service_it_font_br_p_pre_SET_P_U_Enter_User_ID_amp_C_resourcekit_reg_exe_add_HKLM_SOFTWARE_Microsoft_Windows_NT_CurrentVersion_Winlogon_f_v_DefaultUser_Name_d_U_pre_p_Note_Reg_exe_syntax_varies_with_the_reg_exe_version_Type_p_pre_Reg_pre_p_for_more_information_about_the_syntax_Reg_exe_update_might_have_replaced_reg_exe_f_p_p_font_class_subhead_25_How_can_I_see_the_screen_saver_settings_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_desktop_vbs_S_C_pre_p_font_class_subhead_26_How_can_I_get_a_detailed_list_of_drivers_on_my_local_computer_font_br_p_pre_C_resourcekit_drivers_exe_pre_p_font_class_subhead_27_How_can_I_get_a_detailed_list_of_drivers_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_C_pstools_psexec_exe_C_C_C_resourcekit_drivers_exe_pre_p_font_class_subhead_28_How_can_I_determine_the_OS_and_role_of_a_remote_Win2K_or_NT_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_C_resourcekit_gettype_exe_C_pre_p_font_class_subhead_29_How_can_I_get_the_last_boot_up_time_and_installation_date_of_the_OS_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_CScript_nologo_C_resourcekit_listos_vbs_S_C_pre_p_font_class_subhead_30_How_can_I_get_the_installed_printer_information_including_the_default_printer_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_CScript_nologo_C_resourcekit_listprinters_vbs_S_C_pre_p_font_class_subhead_31_How_can_I_view_the_recovery_settings_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_CScript_nologo_C_resourcekit_osreconfig_vbs_S_C_pre_p_font_class_subhead_32_How_can_I_view_the_pagefile_settings_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_CScript_nologo_C_resourcekit_pagefile_vbs_L_S_C_pre_p_font_class_subhead_33_How_can_I_see_which_user_profile_contains_program_menu_shortcuts_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_programgroups_vbs_S_C_pre_p_font_class_subhead_34_How_can_I_view_the_executable_paths_for_processes_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_CScript_nologo_C_resourcekit_ps_vbs_S_C_pre_p_font_class_subhead_35_How_can_I_see_a_quick_top_down_view_of_running_processes_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_pstop_vbs_S_C_pre_p_font_class_subhead_36_How_can_I_view_basic_configuration_information_including_service_packs_and_disk_space_on_a_server_font_br_p_pre_SET_P_C_Enter_computer_name_amp_C_resourcekit_srvinfo_exe_C_pre_p_font_class_subhead_37_How_can_I_see_a_list_of_hotfixes_installed_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_For_f_tokens_2_delims_i_in_C_resourcekit_srvinfo_exe_ns_C_Findstr_0_9_0_9_do_ECHO_i_pre_p_font_class_subhead_38_How_can_I_view_uptime_and_blue_screen_information_on_a_server_font_br_p_pre_SET_P_C_Enter_computer_name_amp_C_resourcekit_uptime_exe_S_A_C_pre_p_font_class_subhead_39_How_can_I_view_thread_information_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_thread_vbs_S_C_pre_p_font_class_subhead_40_How_can_I_determine_whether_the_remote_procedure_call_RPC_hotfix_is_installed_on_a_remote_Win2K_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_C_resourcekit_reg_exe_Query_HKLM_SOFTWARE_Microsoft_Updates_Windows_2000_SP5_KB823980_C_find_I_KB823980_amp_amp_ECHO_SP_Installed_ECHO_SP_not_installed_or_node_not_available_pre_p_font_class_subhead_Disk_Scripting_Commands_font_br_You_can_use_scripts_to_greatly_simplify_removable_and_nonremovable_disk_storage_management_As_with_the_computer_management_scripting_commands_many_of_the_disk_scripting_commands_below_let_you_query_remote_machines_for_information_p_p_font_class_subhead_41_How_can_I_determine_whether_any_of_the_drives_labeled_C_through_I_on_my_main_file_server_has_the_dirty_bit_set_and_will_run_Chkdsk_on_the_next_reboot_font_br_p_pre_For_D_i_in_C_D_E_F_G_H_I_do_Chkntfs_i_pre_p_font_class_subhead_42_How_can_I_prevent_the_possibility_of_a_large_M_drive_having_Chkdsk_run_on_the_next_reboot_font_br_p_pre_Chkntfs_X_M_pre_p_font_class_subhead_43_How_can_I_make_the_Chkdsk_cancellation_timeout_period_30_seconds_instead_of_the_default_10_seconds_to_give_me_more_time_to_stop_Chkdsk_from_running_on_boot_up_font_br_p_pre_Chkntfs_T_30_pre_p_font_class_subhead_44_How_can_I_determine_what_the_Chkdsk_timeout_period_is_set_for_on_a_server_font_br_p_pre_Chkntfs_T_pre_p_font_class_subhead_45_How_can_I_determine_whether_physical_or_logical_disk_counters_are_enabled_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_Diskperf_exe_C_pre_p_font_class_subhead_46_How_can_I_get_information_about_the_CD_ROM_drive_on_another_computer_including_determining_whether_media_is_present_in_the_drive_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_cdromdrives_vbs_S_C_pre_p_font_class_subhead_47_How_can_I_see_the_disk_partition_information_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_amp_CScript_nologo_C_resourcekit_diskpartition_vbs_S_C_pre_p_font_class_subhead_48_How_can_I_get_detailed_physical_drive_information_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_drives_vbs_S_C_pre_p_font_class_subhead_49_How_can_I_see_the_available_free_space_on_drives_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_listfreespace_vbs_S_C_pre_p_font_class_subhead_50_How_can_I_see_the_total_space_capacity_of_drives_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_listspace_vbs_S_C_pre_p_font_class_subhead_51_How_can_I_view_the_SCSI_controller_settings_on_a_remote_computer_font_br_p_pre_SET_P_C_Enter_computer_name_amp_CScript_nologo_C_resourcekit_scsicontroller_vbs_S_C_pre_p_font_class_subhead_Converting_the_Scripting_Commands_into_Batch_Scripts_font_br_Now_that_I_ve_introduced_you_to_the_scripting_commands_you_ll_want_to_consider_the_following_notes_and_advice_when_converting_these_commands_for_use_in_your_own_bat_file_scripts_p_ol_li_To_convert_the_one_line_commands_into_multiline_batch_scripts_copy_the_one_line_of_code_into_a_text_file_and_rename_the_file_with_a_bat_extension_li_li_Put_the_Setlocal_Enabledelayedexpansion_command_at_the_top_of_your_script_before_other_code_Commands_that_are_linked_by_one_ampersand_amp_should_each_be_put_on_one_line_and_the_ampersands_removed_li_li_The_For_command_uses_slightly_different_syntax_when_run_in_a_bat_file_Replace_any_single_percent_signs_with_double_percent_signs_e_g_replace_i_with_i_if_you_re_saving_the_code_into_a_bat_file_If_you_re_copying_and_pasting_code_into_the_command_shell_retain_the_single_percent_signs_li_li_You_can_execute_bat_file_scripts_by_double_clicking_them_or_dragging_them_into_a_command_shell_window_and_executing_them_from_there_by_pressing_Enter_If_you_want_the_command_shell_window_to_stay_open_until_you_hit_another_key_after_you_double_click_your_bat_script_add_the_Pause_command_to_the_bottom_of_your_script_li_li_For_the_start_of_a_10_part_article_series_with_homework_that_will_help_you_get_up_to_speed_on_scripting_and_help_you_get_more_out_of_your_converted_one_line_scripting_commands_see_Shell_Scripting_101_Lesson_1_http_www_winnetmag_com_windowsscripting_InstantDoc_ID_16355_li_li_Get_Timothy_Hill_s_Windows_NT_Shell_Scripting_Que_1998_which_is_the_best_reference_book_for_built_in_Windows_commands_and_resource_kit_utilities_Some_command_syntax_might_have_changed_on_newer_resource_kit_utility_versions_but_this_book_is_still_quite_valuable_li_ol_p_font_class_subhead_File_Share_and_Printer_Scripting_Commands_font_br_You_can_use_scripts_to_create_shares_audit_permissions_and_even_set_up_file_share_resources_on_a_cluster_The_code_samples_below_highlight_many_useful_commands_for_finding_files_matching_certain_criteria_doing_automated_deletions_and_determining_print_driver_information_p_p_font_class_subhead_52_How_can_I_determine_whether_a_share_is_online_font_br_p_pre_SET_P_CS_Enter_servername_sharename_amp_amp_If_Exist_CS_ECHO_CS_online_amp_amp_If_Not_Exist_CS_ECHO_CS_offline_pre_p_font_class_subhead_53_How_can_I_determine_whether_any_xls_files_exist_in_a_folder_font_br_p_pre_If_Exist_computername_sharename_foldername_xls_ECHO_XLS_file_s_exist_pre_p_font_class_subhead_54_How_can_I_delete_all_files_in_the_Temp_folder_font_br_p_pre_Del_Q_S_temp_pre_p_font_class_subhead_55_How_can_I_delete_all_files_more_than_10_days_old_in_the_Temp_folder_font_br_p_pre_Forfiles_p_temp_s_m_d_10_c_CMD_C_DEL_Q_F_0x22_PATH_FILE0x22_pre_p_Note_Forfiles_command_on_Windows_2003_is_different_Type_p_pre_Forfiles_pre_p_for_more_information_about_the_syntax_p_p_font_class_subhead_56_How_can_I_delete_all_empty_folders_in_the_Temp_folder_font_br_p_pre_Forfiles_p_temp_s_m_c_CMD_C_if_ISDIR">,"
  %i in ('CScript //nologo
  company,dc=com" /S:subtree
  /Q /C:"&amp(ObjectCategory=
  computer)"') do ECHO %i,%j>>

6. How can I determine which computer accounts are disabled in my OU?

For /f "tokens=*" %i in
  computer "LDAP://dc=sales,dc=
  yourcompany,dc=com" -disabled
  ') do @ECHO %i

7. How can I find all disabled user accounts in my OU?

For /f "tokens=*" %i in
  user "LDAP://dc=sales,dc=your
  company,dc=com" -disabled')
  do @ECHO %i

Computer Management Scripting Commands
Many of the computer management code examples below let you remotely query and configure systems and eliminate the necessity of physically logging on to every client. Automating your day-to-day computer management tasks can be a major time saver.

8. How can I determine whether a remote computer is on with Ping?

(SET /P C=Enter computer name:
  ) &amp (For /f %i in ('Ping -n 1
  !C!^|Find "Reply"') do @ECHO
  Online!)||(@ECHO Offline!)

9. How can I determine whether a remote computer is on with NBTstat?

(SET /P C=Enter computer name:
  ) &amp (For /f %i in ('NBTstat
  -a !C!^|Find "found."') do
  @ECHO Offline!)||(@ECHO

10. How can I determine whether a remote computer is on with Net Use?

(SET /P C=Enter computer name:
  ) &amp (For /f "tokens=*" %i in
  ('@Net Use \\!C!  2^>NUL ^|
  find "successfully."') do
  @ECHO Online!)||(@ECHO

11. How can I compare the time on the local computer with the logon server?

For /f "tokens=*" %i in ('Net
  Time \\%COMPUTERNAME% ^| find
  /V "command"') do @ECHO
  %i&ampFor /f "tokens=*" %i in
  ('Net Time %LOGONSERVER% ^|
  find /V /I "command"') do
  @ECHO %i

12. How can I compare the time on a remote computer with the logon server?

(SET /P C=Enter computer name:
  ) &amp For /f "tokens=*" %i in
  ('Net Time \\!C! ^| find /V
  "command"') do @ECHO %i&ampFor
  /f "tokens=*" %i in ('Net
  Time %LOGONSERVER% ^| find /V
  "command"') do @ECHO %i

13. How can I find all the computers in my Win2K domain that have the word "Sales" in the computer name?

For /f "tokens=1 delims=$" %i
  in ('C:\resourcekit\global
  .exe "Domain Computers"
  %LOGONSERVER% ^| Find "$" ^|
  Find /i "SALES"') do @ECHO %i

14. How can I find all the computers in my NT 4.0 domain that have the word "Sales" in the computer name?

For /f "tokens=1 delims=$" %i
  in (' C:\resourcekit\global
  .exe "Domain Users" %LOGON
  SERVER% ^| Find "$" ^| Find
  /i "SALES"') do @ECHO %i

15. How can I create a shortcut on my desktop to manage a remote computer?

(SET /P C=Enter computer name
  for shortcut: ) &amp "shortcut
  .exe" -f -t "%SystemRoot%
  \system32\compmgmt.msc" -a
  "/computer=!C!" -d "%System
  Root%\system32" -n "%USER

16. How can I check the trust relationship between a computer and its domain?

(SET /P C=Enter computer name:
  ) &amp C:\pstools\psexec.exe -c
  \\!C! C:\supporttools\netdiag
  .exe /test:trust

17. How can I get system information about a remote computer?

(SET /P C=Enter computer name:
  ) &amp C:\pstools\psinfo.exe

18. How can I see details of all the running processes on a remote computer?

(SET /P C=Enter computer name:
  ) & C:\pstools\pslist.exe

19. How can I see the last five events in the System log on a remote computer?

(SET /P C=Enter computer name:
  ) &amp C:\pstools\psloglist.exe
  -n 5 system \\!C!

20. How can I determine whether the McAfee virus definitions have been updated in the past 7 days?

(SET /P C=Enter computer name:
  ) &amp C:\pstools\psloglist.exe
  -o mclogevent application -d
  7 \\!C!

21. How can I see the past 10 logon events in the System log on a remote computer?

(SET /P C=Enter computer name:
  ) &amp C:\pstools\psloglist.exe
  -i 6009 -n 10 \\!C!

22. How can I see a list of computer accounts that have password ages more than 180 days old?

  /Machines /v /Min:180 /Tabs

23. How can I shut down and restart a remote computer?

(SET /P C=Enter computer name:
  ) &amp C:\resourcekit\shutdown.exe
  \\!C! /R /C

24. How can I change the username in the logon box back to the original username after I log on to a computer and service it?

(SET /P U=Enter User ID: ) &
  C:\resourcekit\reg.exe add
  \Windows NT\CurrentVersion
  \Winlogon" /f /v DefaultUser
  Name /d !U!

Note: Reg.exe syntax varies with the reg.exe version. Type

Reg /?

for more information about the syntax. Reg.exe update might have replaced reg.exe /f.

25. How can I see the screen saver settings on a remote computer?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  /S !C!

26. How can I get a detailed list of drivers on my local computer?


27. How can I get a detailed list of drivers on a remote computer?

(SET /P C=Enter computer name:
  ) &amp C:\pstools\psexec.exe
  \\!C! /C C:\resourcekit

28. How can I determine the OS and role of a remote Win2K or NT computer?

(SET /P C=Enter computer name:
  ) &amp C:\resourcekit\gettype
  .exe \\!C!

29. How can I get the last boot-up time and installation date of the OS on a remote computer?

(SET /P C=Enter computer name:
  ) &amp CScript //nologo
  /S !C!

30. How can I get the installed printer information, including the default printer, on a remote computer?

(SET /P C=Enter computer name:
  ) &amp CScript //nologo
  .vbs /S !C!

31. How can I view the recovery settings on a remote computer?

(SET /P C=Enter computer name:
  ) &amp CScript //nologo
  /S !C!

32. How can I view the pagefile settings on a remote computer?

(SET /P C=Enter computer name:
  ) &amp CScript //nologo
  /L /S !C!

33. How can I see which user profile contains program menu shortcuts on a remote computer?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  .vbs /S !C!

34. How can I view the executable paths for processes on a remote computer?

(SET /P C=Enter computer name:
  ) &amp CScript //nologo
  C:\resourcekit\ps.vbs /S !C!

35. How can I see a quick top-down view of running processes on a remote computer?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  C:\resourcekit\pstop.vbs /S

36. How can I view basic configuration information, including service packs and disk space, on a server?

(SET /P C=Enter computer name:
  ) & C:\resourcekit\srvinfo
  .exe \\!C!

37. How can I see a list of hotfixes installed on a remote computer?

(SET /P C=Enter computer name:
  ) & For /f "tokens=2 delims=
  \[:\]" %i in (' C:\resourcekit
  \srvinfo.exe -ns \\!C! ^|
  Findstr "\[0-9\]\[0-9\]\]:"') do
  @ECHO %i

38. How can I view uptime and blue screen information on a server?

(SET /P C=Enter computer name:
  ) & C:\resourcekit\uptime.exe
  /S /A \\!C!

39. How can I view thread information?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  C:\resourcekit\thread.vbs /S

40. How can I determine whether the remote procedure call (RPC) hotfix is installed on a remote Win2K computer?

(SET /P C=Enter computer name:
  ) & C:\resourcekit\reg.exe
  2000\SP5\KB823980" \\!C!
  |find /I "- KB823980"&&ECHO
  SP Installed!||ECHO SP not
  installed or node not

Disk Scripting Commands
You can use scripts to greatly simplify removable and nonremovable disk storage management. As with the computer management scripting commands, many of the disk scripting commands below let you query remote machines for information.

41. How can I determine whether any of the drives labeled C through I on my main file server has the dirty bit set and will run Chkdsk on the next reboot?

For /D %i in (C D E F G H I) do
  Chkntfs %i:

42. How can I prevent the possibility of a large M drive having Chkdsk run on the next reboot?

Chkntfs /X M:

43. How can I make the Chkdsk cancellation timeout period 30 seconds instead of the default 10 seconds to give me more time to stop Chkdsk from running on boot up?

Chkntfs /T:30

44. How can I determine what the Chkdsk timeout period is set for on a server?

Chkntfs /T

45. How can I determine whether physical or logical disk counters are enabled on a remote computer?

(SET /P C=Enter computer name:
  ) & Diskperf.exe \\!C!

46. How can I get information about the CD-ROM drive on another computer, including determining whether media is present in the drive?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  .vbs /S !C!

47. How can I see the disk partition information on a remote computer?

(SET /P C=Enter computer name:
  ) &amp CScript //nologo
  .vbs /S !C!

48. How can I get detailed physical drive information on a remote computer?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  C:\resourcekit\drives.vbs /S

49. How can I see the available free space on drives on a remote computer?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  .vbs  /S !C!

50. How can I see the total space capacity of drives on a remote computer?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  .vbs  /S !C!

51. How can I view the SCSI controller settings on a remote computer?

(SET /P C=Enter computer name:
  ) & CScript //nologo
  .vbs /S !C!

Converting the Scripting Commands into Batch Scripts
Now that I've introduced you to the scripting commands, you'll want to consider the following notes and advice when converting these commands for use in your own .bat file scripts:

  1. To convert the one-line commands into multiline batch scripts, copy the one line of code into a text file and rename the file with a .bat extension.
  2. Put the Setlocal Enabledelayedexpansion command at the top of your script before other code. Commands that are linked by one ampersand (&) should each be put on one line and the ampersands removed.
  3. The For command uses slightly different syntax when run in a .bat file. Replace any single percent signs (%) with double percent signs (e.g., replace %i with %%i) if you're saving the code into a .bat file. If you're copying and pasting code into the command shell, retain the single percent signs.
  4. You can execute .bat file scripts by double-clicking them or dragging them into a command-shell window and executing them from there by pressing Enter. If you want the command-shell window to stay open until you hit another key after you double-click your .bat script, add the Pause command to the bottom of your script.
  5. For the start of a 10-part article series with homework that will help you get up to speed on scripting and help you get more out of your converted one-line scripting commands, see "Shell Scripting 101, Lesson 1,", InstantDoc ID 16355.
  6. Get Timothy Hill's Windows NT Shell Scripting (Que, 1998), which is the best reference book for built-in Windows commands and resource kit utilities. Some command syntax might have changed on newer resource kit utility versions, but this book is still quite valuable.

File, Share, and Printer Scripting Commands
You can use scripts to create shares, audit permissions, and even set up file share resources on a cluster. The code samples below highlight many useful commands for finding files matching certain criteria, doing automated deletions, and determining print driver information.

52. How can I determine whether a share is online?

(SET /P CS=Enter \\servername\sharename: )
&amp (If Exist !CS! ECHO !CS! online) &amp
(If Not Exist !CS! ECHO !CS! offline)

53. How can I determine whether any .xls files exist in a folder?

If Exist \\computername\sharename\foldername\*.xls ECHO XLS file(s) exist

54. How can I delete all files in the Temp folder?

Del /Q /S %temp%\*.*

55. How can I delete all files more than 10 days old in the Temp folder?

Forfiles -p%temp% -s -m*.* -d-10 -c"CMD /C DEL /Q /F 0x22@PATH\@FILE0x22"

Note: Forfiles command on Windows 2003 is different. Type

Forfiles /?

for more information about the syntax.

56. How can I delete all empty folders in the Temp folder?

Forfiles -p%temp% -s -m* -c"CMD /C if @ISDIR
TRUE RMDIR 0x22@PATH\@FILE0x22 2>NUL&&echo
Deleted @RELPATH\@FILE||Echo Deletion skipped"

Note: Forfiles command on Windows 2003 is different. Type

Forfiles /?

for more information about the syntax.

57. How can I determine how many .xls files are in a directory?

(SET /P TL=Enter target folder location: )
& SET counter=0 & For /f "tokens=1,2 delims=."
%i in ('dir /b !TL!\*.xls') do @set /A counter+=1&@echo ) %i.%j

58. How can I change all filenames in a specified folder to lowercase?

For /f "tokens=*" %i in
('Dir /l /b /a-d C:\myfolder') do Ren "C:\myfolder\%i" "%i"

59. How can I determine the kilobyte size for a particular file?

For /f "tokens=4" %i in
('Dir /a-d /-c C:\windows\system32\ntoskrnl.exe ^|Find /i " ntoskrnl.exe "')
Do (Set /A size=%i/1024)

Note: The Set /A command has a limitation of nine-digit figures (maximum of 999999999). If you attempt math operations involving more than nine digits (numbers are limited to 32 bits of precision), you might get inaccurate results.

60. How can I determine how many files are in a folder?

Set Counter=& For /f "tokens=*" %i in
('Dir /a-d /b %temp%') do @Set /A Counter+=1 & Echo )%i

61. How can I create a new shared file resource on my cluster?

(CLUSTER clustername RESOURCE Sharename
/Create /Group:"GrpName" /Type:"File Share")
& (CLUSTER clustername RESOURCE Sharename
  /PrivProp Path="R:\MysharedFolder")
& (CLUSTER clustername RESOURCE Sharename
  /PrivProp ShareName="Sharename")
& (CLUSTER clustername RESOURCE Sharename
  /AddDependency:"Disk R:")
& (CLUSTER clustername RESOURCE Sharename
  /Prop Description="Here is my description")

62. How can I locate any open files on a remote computer?

(SET /P C=Enter computer name: ) & C:\pstools\psfile.exe \\!C!

63. How can I view the shares and their permissions settings on a remote computer?

(SET /P C=Enter computer name: ) & C:\resourcekit  srvcheck.exe \\!C!

64. How can I get the properties on a remote shared printer?

CScript //nologo C:\resourcekitprncfg.vbs -g -b \\servername\printer

65. How can I see only the hidden shares on a remote computer?

(SET /P C=Enter computer name: )
& C:\resourcekit\rmtshare.exe \\!C!|sort|find "$"

Hardware Scripting Commands
Finding out what BIOS version, as well as which network card, video card, or mouse is installed on one or more machines can really help you troubleshoot client driver problems. You can use the commands below to help gather this information.

66. How can I check the BIOS on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\checkbios.vbs /S !C!

67. How can I see the display adapter properties on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo  C:\resourcekit\listdisplayconfig.vbs /S !C!

68. How can I get the video card information, including the screen resolution, on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\lstdpconinfo.vbs /S !C!

69. How can I view the pointing device settings on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\pointdev.vbs /S !C!

70. How can I view the processor information on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\processor.vbs /S !C!

71. How can I view the serial port configuration on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\serialport.vbs /S !C!

72. How can I view the audio device configuration on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\sounddevice.vbs /S !C!

73. How can I view tape drive information?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\tapedrive.vbs /S !C!

Networking Scripting Commands
Today's complex networked environments often can be difficult to troubleshoot without some scripted approaches. In some cases, physically logging on to a user's computer to view the network settings might be the only option for which you have no working network interface. In many cases, you can use some simple scripting techniques to capture network settings, identify problems, and get things back on track.

74. How can I see the results of the Ipconfig /All command on a remote computer?

(SET /P C=Enter computer name: )
& C:\pstools\psexec.exe \\!C! IPConfig /ALL

75. How can I get the media access control (MAC) address on a remote machine?

(SET /P C=Enter computer name: ) & C:\resourcekitgetmac.exe \\!C!

76. How can I see network adapter properties on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\listadapters.vbs /S !C!

77. How can I get network protocol information on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\networkprotocol.vbs /S !C!

78. How can I view the protocol bindings on a remote computer?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\protocolbinding.vbs /S !C!

Scripting Environment Scripting Commands
With a few simple techniques, you can make your scripting environment easier to work in. The commands below can make your scripting environment easier to create and run your scripts in.

79. How can I get Notepad to save my settings (e.g., font size) between sessions?

C:\resourcekit\reg.exe Add HKCU\SoftwareMicrosoft\Notepad /v fSavePageSettings
/t REG_DWORD /d "00000001"

Note: Reg.exe syntax varies with the reg.exe version. Type

Reg /?

for more information about the syntax. Reg.exe update might have replaced reg.exe /f.

80. How can I create a desktop shortcut to the Command Shell online Help files on my Win2K computer?

C:\shortcut.exe -f -t "%SYSTEMROOT%hh.exe" -d "%SystemRoot%" -a
"ms-its:%WINDIR%\Help\windows.chm::/ntcmds.htm" -n

81. How can I create a desktop shortcut to the Command Shell online Help files on my XP computer?

C:\shortcut.exe -f -t "%windir%\hh.exe" -d "%windir%" -a
"ms-its:%windir%\Help\ntcmds.chm::/ntcmds.htm" -n

82. How can I find out how long a command or script takes to run?

C:\resourcekit\timethis.exe dir /S /AD /B

Security Scripting Commands
Security is the underpinning of almost everything we do as systems administrators. You can use the commands below to script many of the routine security procedures that administrators face every day.

83. How can I connect to another machine by using different credentials than I'm logged on with?

(SET /P C=Enter computer name: )
& (SET /P U=Enter user name: ) & NET USE \\!C!\IPC$ * /USER:!U!

84. How can I retrieve the machine SID for a remote computer?

(SET /P C=Enter computer name: ) & C:\pstools\psgetsid.exe \\!C!

85. How can I view a list of users who have remote access permissions for a domain?

(SET /P D=Enter Domain name: ) & C:\resourcekit\rasusers.exe %D%

86. How can I send a Net Send message to all members of the local Administrators group while logged on to a computer?

For /f "tokens=1,2 delims=\ " %i in ('C:\resourcekit\showmbrs.exe
\\%computername%\administrators ^| Find /v "Members"')
Do Net Send %j Test Message

Services Scripts
Although you can use the MMC Computer Management snap-in to determine which services are running and to start and stop services, doing so becomes more difficult when you need to query or control multiple nodes. Also, new security concerns require administrators to more closely monitor what services are installed and running. The scripting commands below can empower administrators by letting them quickly gather important service data without having to click through the snap-in for each machine.

87. How can I get a list of services on a computer?

(SET /P C=Enter computer name: ) & C:\resourcekit\netsvc.exe \\!C! /List

88. How can I determine whether the Web service is running on a server?

(SET /P C=Enter computer name: ) & C:\resourcekit\netsvc.exe \\!C! W3SVC /query

89. How can I view a list of stopped services on a remote computer?

(SET /P C=Enter computer name: ) & C:\resourcekit\sclist.exe -s !C!

Users and Groups Scripting Commands
User and group creation and auditing operations can consume much of an enterprise administrator's time. Fortunately, the code below contains a wealth of user and group-related utilities that are available.

90. How can I determine how many groups I'm in?

Set cntr=0&for /f "tokens=1,2,3,4 delims=\[\]" %i in
('C:\joeware\memberof.exe -q -u %USERDOMAIN%\%USERNAME%')
do (SET /A cntr+=1) & (ECHO ^) %l)

91. How can I determine how many groups another user is in?

(SET /P USER=Enter domainname\username: )
& set cntr=0&for /f "tokens=1,2,3,4 delims=\[\]"
%i in ('C:\joeware\memberof.exe -q -u %USER%')
do (SET /A cntr+=1) & (ECHO ^) %l)

92. How can I tell when a domain user account expires (same domain as the logged-on user)?

(SET /P U=Enter user name: ) & C:\joeware\accexp.exe
 !U! -q -s %LOGONSERVER%

93. How can I determine who's logged on locally to a remote computer?

(SET /P C=Enter computer name: ) & C:\pstoolspsloggedon.exe -L \\!C!

94. How can I see a history of the users logged on to a computer?

(SET /P C=Enter computer name: ) &
C:\systools\netusers.exe \\!C! /history /local

95. How can I see a list of user accounts that have password ages between 90 and 180 days old?

C:\systools\netpwage.exe /Users /v /Min: 90 /Max:180 /Tabs

96. How can I find out the members of a Local group?

C:\resourcekit\local.exe "groupname" domainname

97. How can I find out the members of a Global group?

C:\resourcekit\global.exe "groupname" domainname

98. How can I tell what OU my user account is in?

C:\joeware\username.exe -Q -format 1

99. How can I view a list of users associated with running processes on a remote computer?

(SET /P C=Enter computer name: )
& C:\resourcekit\pulist.exe \\!C!

100. How can I view the startup folders for all users and in user profiles?

(SET /P C=Enter computer name: )
& CScript //nologo C:\resourcekit\startup.vbs /S !C!

101. How can I view basic user account information including account lockout?

(SET /P DU=Enter domain\Username: )
& For /f "tokens=1,2 delims=\" %i in ("%DU%")
do CScript //nologo C:\resourcekit\useraccount.vbs /D %i /N %j