Batch Script
A Batch Script is a piece of system-level program logic which can control specific system functions via a command prompt (most commonly Batch is supported in Windows server environments but may be ported to others).
Contents
PowerShell
- How to Write Your First PowerShell Script: https://www.pdq.com/blog/powershell-how-to-write-your-first-powershell-script/
- Windows PowerShell - Owner's Manual: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ee221100(v=msdn.10)
- How to run a PowerShell script?: https://stackoverflow.com/questions/2035193/how-to-run-a-powershell-script (instructions for running from within PowerShell .\MyScript.ps1 and CMD prompt powershell -noexit "& ""C:\my_path\yada_yada\MyScript.ps1""")
- How do you execute an arbitrary native command from a string (in PowerShell)?: https://stackoverflow.com/questions/6338015/how-do-you-execute-an-arbitrary-native-command-from-a-string
- 'Effective Windows PowerShell -- The Free eBook: https://rkeithhill.wordpress.com/2009/03/08/effective-windows-powershell-the-free-ebook/
- Learn Windows PowerShell in a Month of Lunches: https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches
- PowerShell launch script in new instance: https://stackoverflow.com/questions/23237473/powershell-launch-script-in-new-instance
- Starting File Explorer from PowerShell in the current folder: https://4sysops.com/archives/starting-file-explorer-from-powershell-in-the-current-folder/
- How to append to a file with PowerShell: https://www.tachytelic.net/2018/11/append-file-powershell/
- Previous versions in Windows PowerShell: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ee221100(v=msdn.10)
- How to change a File's Date/Timestamp in Windows: https://www.winhelponline.com/blog/change-accessed-modified-created-file-date-timestamp-windows/
- Using Variables to Store Objects: https://docs.microsoft.com/en-us/powershell/scripting/getting-started/fundamental/using-variables-to-store-objects?view=powershell-6
- Windows Powershell The Power of Variables: https://technet.microsoft.com/en-us/library/2007.03.powershell.aspx
- Passing a variable to a PowerShell script via command line: https://stackoverflow.com/questions/16426688/passing-a-variable-to-a-powershell-script-via-command-line
- How to run a Powershell script from the command line and pass a directory as a parameter: https://stackoverflow.com/questions/13724940/how-to-run-a-powershell-script-from-the-commandvaria-line-and-pass-a-directory-as-a-p
- How do I get the current username in Windows PowerShell?: https://stackoverflow.com/questions/2085744/how-do-i-get-the-current-username-in-windows-powershell
- PowerShell Basics -- Working with the If Statement: https://mcpmag.com/articles/2016/03/09/working-with-the-if-statement.aspx
- Powershell, using contains to check If files "contain a certain word": https://stackoverflow.com/questions/18633666/powershell-using-contains-to-check-if-files-contain-a-certain-word
- Powershell - Everything you wanted to know about the IF statement: https://powershellexplained.com/2019-08-11-Powershell-if-then-else-equals-operator/
[6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
- How to loop through a PowerShell array: https://www.spguides.com/powershell-loop-through-array/
- Back to Basics - The PowerShell Foreach Loop: https://adamtheautomator.com/powershell-foreach/
- PowerShell Basics -- Loops – Do… Until .vs. Do…While: https://www.computerperformance.co.uk/powershell/loops-do-while/
- PowerShell Arrays: https://ss64.com/ps/syntax-arrays.html
- PowerShell Basics: Programming With Loops: http://www.tomsitpro.com/articles/powershell-for-loop,2-845.html
- PowerShell -- Escape characters, Delimiters and Quotes: https://ss64.com/ps/syntax-esc.html
- How do I concatenate strings and variables in PowerShell?: https://stackoverflow.com/questions/15113413/how-do-i-concatenate-strings-and-variables-in-powershell
- Powershell -- Everything you wanted to know about variable substitution in strings: https://powershellexplained.com/2017-01-13-powershell-variable-substitution-in-strings/
- How To Test Variables in PowerShell: https://mcpmag.com/articles/2015/12/14/test-variables-in-powershell.aspx
- The call operator (&) allows you to execute a command, script or function: https://ss64.com/ps/call.html
- How do I capture the output into a variable from an external process in PowerShell?: https://stackoverflow.com/questions/8097354/how-do-i-capture-the-output-into-a-variable-from-an-external-process-in-powershe/35980675
- Passing value of variable into Get-Content: https://powershell.org/forums/topic/passing-value-of-variable-into-get-content/
- How to Use PowerShell to Check if a File Exists: https://www.itechguides.com/powershell-check-if-file-exists/
- Find line that contains certain text (even large files, as fast/efficient as possible): https://stackoverflow.com/questions/21310538/get-all-lines-containing-a-string-in-a-huge-text-file-as-fast-as-possible[23][24]
- How To Replace Text in a File with PowerShell: https://mcpmag.com/articles/2018/08/08/replace-text-with-powershell.aspx
- Use Windows PowerShell to search for files: https://devblogs.microsoft.com/scripting/use-windows-powershell-to-search-for-files/ | DOCS -- Get-ChildItem
[28] [29] [30] [31] [32] [33] [34] [35] [36]
- Add-Content - append to specific line: https://stackoverflow.com/questions/31957901/add-content-append-to-specific-line
- Backtick Line Break special characters in PowerShell: https://4sysops.com/archives/the-backtick-line-breaks-in-powershell/[37][38][39][40]
- How to Append Data to a Text File Using PowerShell: https://www.faqforge.com/powershell/append-data-text-file-using-powershell/
- Replacing Contents of a Text File Using Powershell: https://stackoverflow.com/questions/31010480/replacing-contents-of-a-text-file-using-powershell
- PowerShell (DOCS): https://docs.microsoft.com/en-us/powershell/scripting/powershell-scripting?view=powershell-6 | EXAMPLE -- Get-Date[41][42][43]
- Read file line by line in PowerShell: https://stackoverflow.com/questions/33511772/read-file-line-by-line-in-powershell
- How do I write a text file on PowerShell?: https://www.quora.com/How-do-I-write-a-text-file-on-PowerShell
- How To Replace Text in a File with PowerShell: https://mcpmag.com/articles/2018/08/08/replace-text-with-powershell.aspx
- Use PowerShell to Replace Text in Strings: https://devblogs.microsoft.com/scripting/use-powershell-to-replace-text-in-strings/
- Invoke-WebRequest -- Parsing HTML Webpages with Powershell: http://woshub.com/parsing-html-webpages-with-powershell/
- Using the Split Method in PowerShell: https://devblogs.microsoft.com/scripting/using-the-split-method-in-powershell/
- Splitting a string into separate variables: https://stackoverflow.com/questions/30617758/splitting-a-string-into-separate-variables
- PoweSshell script to open web address URL with default browser: https://lockevn.wordpress.com/2011/05/06/powershell-script-to-open-web-address-url-with-default-browser/
- PowerShell cURL – Yes It Exists – Invoke-RestMethod: https://discoposse.com/2012/06/30/powershell-invoke-restmethod-putting-the-curl-in-your-shell/
- Downloading Files with cURL, Including Text and Binary Objects: https://discoposse.com/2016/03/09/downloading-text-and-binary-objects-with-curl/
- Powershell v3 Invoke-RestMethod Headers: https://stackoverflow.com/questions/18278977/powershell-v3-invoke-restmethod-headers
- Update-Help on a Server-core behind a proxy: https://serverfault.com/questions/569301/update-help-on-a-server-core-behind-a-proxy
- Building a Wait-Action function in PowerShell: https://mcpmag.com/articles/2018/03/16/wait-action-function-powershell.aspx
- How-To -- Create and use PowerShell Arrays: https://ss64.com/ps/syntax-arrays.html
- Reading (simple) JSON file using PowerShell: https://digitalexpedient.com/2020/07/reading-json-file-using-powershell/
- Working with (nested/array-containing) JSON data in PowerShell: https://devblogs.microsoft.com/scripting/working-with-json-data-in-powershell/
- Signing PowerShell Scripts: https://www.hanselman.com/blog/SigningPowerShellScripts.aspx
- Windows Environment Variables in PowerShell (getting/setting): https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.1
- How to Reuse Windows PowerShell Functions in Scripts: https://devblogs.microsoft.com/scripting/how-to-reuse-windows-powershell-functions-in-scripts/
- Prompting for User Input with PowerShell: https://www.itprotoday.com/powershell/prompting-user-input-powershell
Windows Terminal
Supports PowerShell, standard DOS-style "command prompt", and others; all with multi-tab support for managing several terminal windows at once.
- Getting Started with Windows Terminal: https://devblogs.microsoft.com/commandline/getting-started-with-windows-terminal/
Commands
To pass a specific command-line input parameter from a Batch script to a called program within it, use:
%1 %2 %3 ... %15
For the first, second, third and fifteenth parameter (etc).
To pass through all command-line input parameters from a Batch script to a called program within it, use:
%*
Resources
- wikipedia: Autorun.inf[53]
- Runas: http://technet.microsoft.com/en-us/library/bb490994.aspx (run a command as another user, i.e. to elevate to Admin privileges, etc)
- Replace.exe Bulk File Find / Replace Command Line Tool: http://www.bestcode.com/html/findreplace.html
- wget for windows: http://gnuwin32.sourceforge.net/packages/wget.htm
- curl for windows: http://curl.haxx.se/download.html#Win32MSVC
- Spring Batch (REFERENCE): http://cdn.dzone.com/sites/all/files/refcardz/rc187-010d-spring-batch.pdf
- The DIR command: http://www.robvanderwoude.com/ntdir.php[54]
- DOS Batch -- Real-world Examples: http://www.dostips.com/DtCodeBatchFiles.php
- PsExec v2.2: https://docs.microsoft.com/en-ca/sysinternals/downloads/psexec (like telnet but for running processes on a remote Windows machine from command-line)[55]
- What is Windows Terminal?: https://docs.microsoft.com/en-us/windows/terminal/
Tutorials
- MS-DOS Batch Script tutorial: https://cs.senecac.on.ca/~albert.pang/ios100/dosbatch.html
- Using command redirection operators: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true
- How to unpack a tar file in windows: http://www.haskell.org/haskellwiki/How_to_unpack_a_tar_file_in_windows
- Is there replacement for cat on Windows: http://stackoverflow.com/questions/60244/is-there-replacement-for-cat-on-windows
- Remote Desktop - shortcut gone, where to find exe file?: http://www.softwaretipsandtricks.com/forum/windows-xp/23208-remote-desktop-shortcut-gone-where-find-exe-file.html (c:\windows\system32\mstsc.exe)
- Windows Command Line Environment Variables: http://libertyboy.free.fr/computing/reference/envariables/
- How to create ls in windows command prompt?: http://stackoverflow.com/questions/9362692/how-to-create-ls-in-windows-command-prompt#15043472
- How to find out what file is being used by another process: https://stackoverflow.com/questions/24823010/how-to-find-out-what-file-is-being-used-by-another-process
- How to Set an Environment Variable in Windows - Command Line & Registry: https://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-windows-command-line-and-registry/
- Windows Batch Scripting -- If/Then Conditionals: https://steve-jansen.github.io/guides/windows-batch-scripting/part-5-if-then-conditionals.html
- Batch How To -- Display & Redirect Output: http://www.robvanderwoude.com/battech_redirection.php
- Adding an alias in Windows 7 or making ls = dir in a command prompt: http://www.rhyous.com/2010/10/20/adding-an-alias-in-windows-7-or-making-ls-dir-in-a-command-prompt
- How to create an empty file (i.e. ".filetype" with no filename) at the command line in Windows?: https://stackoverflow.com/questions/1702762/how-to-create-an-empty-file-at-the-command-line-in-windows
- Create/rename a file/folder that begins with a dot in Windows?: https://superuser.com/questions/64471/create-rename-a-file-folder-that-begins-with-a-dot-in-windows
- How to empty out (DELETE CONTENTS OF) a folder in a command prompt? (Windows7): https://superuser.com/questions/198078/how-to-empty-a-folder-in-a-command-prompt-windows7
- Command Prompt - outputting results without overwriting target file: https://stackoverflow.com/questions/6065732/command-prompt-outputting-results-without-overwriting-target-file#6065745
- Grep equivalent for Windows 7?: https://superuser.com/questions/300815/grep-equivalent-for-windows-7
- Command Prompt Won't Change Directory: https://stackoverflow.com/a/11065668/335867 (if the standard "cd d:" doesn't work for you then try cd /d D:\)
- How do I make a batch file replace a sub string within a text file without changing it's layout?: https://superuser.com/questions/1281743/how-do-i-make-a-batch-file-replace-a-sub-string-within-a-text-file-without-chang
- Is there an equivalent of 'which' on the Windows command line?: https://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-the-windows-command-line#304447
- Windows -- Show Proxy Settings – CMD & PowerShell: https://www.shellhacks.com/windows-show-proxy-settings-cmd-powershell/
- Resolve-DnsName: https://docs.microsoft.com/en-us/powershell/module/dnsclient/resolve-dnsname?view=win10-ps
- Resolve-DnsName - Formatting and Error Catching: https://stackoverflow.com/questions/31257346/resolve-dnsname-formatting-and-error-catching
- How to Get Tail Like Functionality on Windows with PowerShell: https://www.howtogeek.com/tips/how-to-get-tail-like-functionality-on-windows-with-powershell/
- Timestamp in batch file (using %date% %time%): https://community.spiceworks.com/scripts/show/1223-getting-current-timestamp-in-batch-file[56][57]
External Links
- wikipedia: Batch file
- Windows batch files: .bat vs .cmd?: https://stackoverflow.com/questions/148968/windows-batch-files-bat-vs-cmd?rq=1
- Batch script to delete file older than 30 days: http://en.kioskea.net/forum/affich-73016-batch-script-to-delete-file-older-than-30-day
- How to determine which program uses or blocks specific transmission control protocol ports in Windows: https://support.microsoft.com/en-us/help/323352/how-to-determine-which-program-uses-or-blocks-specific-transmission-control-protocol-ports-in-windows-server-2003 (this command lists PIDs by Port netstat -ano, then match up name via Task Manager --> Processes tab, using the PID)
- How to Kill Process that is Running on Certain Port in Windows?: http://laksmono.com/2009/12/02/how-to-kill-process-that-is-running-on-certain-port-in-windows/
- Easily identify services running inside Windows XP's processes: http://www.techrepublic.com/article/easily-identify-services-running-inside-windows-xps-processes/
- NetStat: http://support.microsoft.com/kb/281336 (help file)
- Windows Command Line Tips: http://www.scribd.com/doc/511785/Notes-Windows-Command-Line-Tips
- Getting the Most out of Windows Command Prompt: http://www.powercram.com/2009/12/getting-most-out-of-windows-command.html
- TCP/IP and Networking Tools: http://commandwindows.com/tcpiputil.htm
- Is there anything that Windows does better than OS X?: http://www.quora.com/Is-there-anything-that-Windows-does-better-than-OS-X (ANSWER: PowerShell)
- A Comparison of Common DOS and Linux Commands: https://www.centos.org/docs/rhel-sbs-en-3/ap-doslinux.html
- "dir" vs "ls": http://allinthehead.com/retro/131/dir-vs-ls
References
- ↑ Microsoft dev docs -- PowerShell - Start-Process: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.1
- ↑ Programmatically Opening Windows Terminal in a Specific Folder: https://weblog.west-wind.com/posts/2019/Sep/03/Programmatically-Opening-Windows-Terminal-in-a-Specific-Folder
- ↑ What is the best way for communication between Powershell script and Windows service?: https://stackoverflow.com/questions/41017341/what-is-the-best-way-for-communication-between-powershell-script-and-windows-ser
- ↑ PowerTip - Open Windows Explorer to a Location: https://devblogs.microsoft.com/scripting/powertip-open-windows-explorer-to-a-location/
- ↑ PowerTip - Use PowerShell to Create New File: https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-create-new-file/
- ↑ Everything you wanted to know about the if statement: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-if?view=powershell-7.1
- ↑ Powershell - If Else Statement: https://www.tutorialspoint.com/powershell/if_else_statement_in_powershell.htm
- ↑ Check if a string is NULL or EMPTY using PowerShell: https://techibee.com/powershell/check-if-a-string-is-null-or-empty-using-powershell/1889
- ↑ How to use If-ElseIf-Else in PowerShell: https://teckangaroo.com/how-to-use-else-if-in-powershell/
- ↑ PowerShell - What is the best way to check whether the current user has permission to overwrite a file in PowerShell?: https://stackoverflow.com/questions/22943289/powershell-what-is-the-best-way-to-check-whether-the-current-user-has-permissio
- ↑ How to declare a variable and its type is Boolean in PowerShell?: https://stackoverflow.com/questions/54721256/how-to-declare-a-variable-and-its-type-is-boolean-in-powershell
- ↑ Understanding Booleans in PowerShell: https://social.technet.microsoft.com/wiki/contents/articles/2286.understanding-booleans-in-powershell.aspx
- ↑ Boolean Values and Operators: https://devblogs.microsoft.com/powershell/boolean-values-and-operators/
- ↑ Powershell – If -And' & If -Or Statements: https://www.heelpbook.net/2019/powershell-if-and-if-or-statements/
- ↑ Introduction to PowerShell If-Not: https://www.educba.com/powershell-if-not/
- ↑ PowerShell if-then Latest examples 2020: https://teckangaroo.com/how-to-use-if-then-in-powershell/
- ↑ Back to Basics -- The PowerShell For Loop: https://adamtheautomator.com/powershell-for-loop/
- ↑ Powershell For Loop Explained -- syntax & examples: https://www.itechguides.com/powershell-for-loop-explained-syntax-and-examples/
- ↑ PowerShell Basics -Contains, -CContains & -NotContains Conditional Operators: https://www.computerperformance.co.uk/powershell/contains/
- ↑ More Powerful Batch Files Part I - Branching and Looping: http://commandwindows.com/batchfiles-branching.htm
- ↑ Working with Values and Variables in PowerShell: https://mcpmag.com/articles/2012/10/02/pshell-values-variables.aspx
- ↑ Create a File If Not Exists via PowerShell: https://morgantechspace.com/2015/06/powershell-create-file-if-not-exists.html
- ↑ Return line number from string match: https://stackoverflow.com/questions/34664231/return-line-number-from-string-match
- ↑ PowerShell: Searching Through Files for Matching Strings: https://www.pdq.com/blog/powershell-searching-through-files-for-matching-strings/
- ↑ Find files which does not contains selected string: https://stackoverflow.com/questions/17945514/find-files-which-does-not-contains-selected-string
- ↑ Powershell (how to check whether) "string does not contain" some value: https://stackoverflow.com/questions/27970441/powershell-string-does-not-contain
- ↑ Microsoft dev docs -- PowerShell - About Try Catch Finally: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally
- ↑ PowerTip: Use PowerShell to Find Folders that Match Pattern: https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-find-folders-that-match-pattern/
- ↑ Get-ChildItem - List files with the matched string pattern ?: https://social.technet.microsoft.com/Forums/en-US/617c4f1c-786e-45a8-85e0-d9680bd470af/getchilditem-list-files-with-the-matched-string-pattern-
- ↑ Find Files That Match a Pattern: http://powershellcookbook.com/recipe/lvCW/find-files-that-match-a-pattern
- ↑ Grep for Windows – findstr example: https://mkyong.com/linux/grep-for-windows-findstr-example/
- ↑ "find" command: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find
- ↑ How to execute find command with Powershell: https://stackoverflow.com/questions/37639919/how-to-execute-find-command-with-powershell
- ↑ Why can “find” not be used in PowerShell?: https://superuser.com/questions/1208964/why-can-find-not-be-used-in-powershell
- ↑ EXAMPLE command to find a specific pattern recursively in all sub-folders under a specific path (Get-ChildItem -Path 'C:\APPS\AEM\author\crx-quickstart\launchpad\felix' -Force -Recurse -ErrorAction 'SilentlyContinue' -Filter "classes").FullName
- ↑ Find PowerShell Commands by Using the Get-Command Cmdlet: https://devblogs.microsoft.com/scripting/find-powershell-commands-by-using-the-get-command-cmdlet/
- ↑ How to insert a newline before adding content with add-content: https://social.technet.microsoft.com/Forums/sharepoint/en-US/caa9c4ef-40cd-444e-ab4b-c1ca674361c0/how-to-insert-a-newline-before-adding-content-with-addcontent?forum=winserverpowershell
- ↑ Writing new lines to a text file in PowerShell: https://stackoverflow.com/questions/17434151/writing-new-lines-to-a-text-file-in-powershell
- ↑ PowerTip - New Lines with PowerShell: https://blogs.technet.microsoft.com/heyscriptingguy/2014/09/07/powertip-new-lines-with-powershell/
- ↑ How to - Out file to New Line in Text File: https://community.spiceworks.com/topic/2002837-how-to-out-file-to-new-line-in-text-file
- ↑ Convert String to 24 Hour time format in powershell: https://stackoverflow.com/questions/12162702/convert-string-to-24-hour-time-format-in-powershell
- ↑ Standard DateTime Format patterns:: https://ss64.com/ps/syntax-dateformats.html
- ↑ PowerShell: Parsing Date and Time: http://dusan.kuzmanovic.net/2012/05/07/powershell-parsing-date-and-time/
- ↑ PowerShell parsing JSON: https://stackoverflow.com/questions/45945944/powershell-parsing-json (more readable/reusable script example)
- ↑ PowerShell Basics -- Environment Variables -$Env: (examples & scripts): https://www.computerperformance.co.uk/powershell/environmental-variables/
- ↑ Speed Up Array Comparisons in Powershell with a Runtime Regex: https://devblogs.microsoft.com/scripting/speed-up-array-comparisons-in-powershell-with-a-runtime-regex/
- ↑ Microsoft dev docs -- Powershell - Everything you wanted to know about array: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.1
- ↑ PowerTip - Prompt for Input for PowerShell Script: https://devblogs.microsoft.com/scripting/powertip-prompt-for-input-for-powershell-script/
- ↑ Windows Terminal Tips and Tricks: https://devblogs.microsoft.com/commandline/windows-terminal-tips-and-tricks/
- ↑ Set up Powerline in Windows Terminal: https://docs.microsoft.com/en-us/windows/terminal/tutorials/powerline-setup
- ↑ Cascadia Code 2009.22: https://github.com/microsoft/cascadia-code/releases (better look & feel font to be used with Consoles)
- ↑ Is there a way to indicate the last n parameters in a batch file?: http://stackoverflow.com/questions/761615/is-there-a-way-to-indicate-the-last-n-parameters-in-a-batch-file
- ↑ http://stackoverflow.com/questions/12606609/autorun-inf-doesnt-work
- ↑ Tips for using DIR: http://www.robvanderwoude.com/dir.php
- ↑ Run batch scripts on a remote server (windows) from jenkins: https://stackoverflow.com/questions/22553588/run-batch-scripts-on-a-remote-server-windows-from-jenkins
- ↑ How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?: https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format
- ↑ How to get a UNIVERSAL Windows batch file timestamp: https://stackoverflow.com/questions/1747468/how-to-get-a-universal-windows-batch-file-timestamp
See Also
Shell Script | Windows | DOS