{"id":1230,"date":"2014-10-23T14:30:09","date_gmt":"2014-10-23T13:30:09","guid":{"rendered":"https:\/\/www.autoitconsulting.com\/site\/?p=1230"},"modified":"2025-07-14T22:51:05","modified_gmt":"2025-07-14T21:51:05","slug":"get-current-script-directory-powershell-vbscript-batch","status":"publish","type":"post","link":"https:\/\/www.autoitconsulting.com\/site\/scripting\/get-current-script-directory-powershell-vbscript-batch\/","title":{"rendered":"Get the Current Script Directory in PowerShell, VBScript and Batch"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>This post shows how to quickly get the current script directory using PowerShell, VBScript and Batch &#8211; the most commonly used scripting languages for Windows.<\/p>\n\n\n\n<p>The scripts I write usually read in other files or call other scripts. In order for these scripts to run from any location &#8211; such as a UNC path &#8211; without hard coding paths they need to use relative paths. Depending on how the script is called the working directory may not be the same as the script file. For example, if your current directory is C:\\Windows and you run the script \\\\server\\share\\somepath\\myscript.cmd then any relative paths in the script file won&#8217;t&nbsp;work correctly.<\/p>\n\n\n\n<p>One way around this is to make the script change working directory right at the start and then use relative paths after that. But in some situations &#8211; such as batch files on UNC paths &#8211; this won&#8217;t aways work. The best way to get around this is to determine the directory that the script resides in at the start of the script and then make all other paths reference that.<\/p>\n\n\n\n<p>Because I jump around various scripting languages all the time, I tend to forget the best way to do this and have to hunt for examples in old scripts. As a reference for myself this post gives the template for getting the current script directory in the languages I tend to use: PowerShell, VBScript and batch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Windows Batch<\/h2>\n\n\n\n<p>Windows batch is the trickiest in some ways &#8211; it also is the one that cannot support UNC working directories. There is a built-in variable <strong>%~dp0<\/strong> which expands to the path that the script is located in&nbsp;<strong>including the trailing slash<\/strong>. This can make for messy looking scripts because to run&nbsp;<strong>setup.exe<\/strong> from the current script directory you would use&nbsp;<strong>%~dp0setup.exe<\/strong>. This works great but can be a little confusing for others to read because it looks like a typo.<\/p>\n\n\n\n<p>My preferred method is to create a new variable at the top of the script using %~dp0 and then stripping the trailing backslash. Here is the script:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">BAT (Batchfile)<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>@ECHO OFF\nREM Determine script location for Windows Batch File\n\nREM Get current folder with no trailing slash\nSET ScriptDir=%~dp0\nSET ScriptDir=%ScriptDir:~0,-1%\n\nECHO Current script directory is %ScriptDir%<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">@<\/span><span style=\"color: #569CD6\">ECHO<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">OFF<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">REM<\/span><span style=\"color: #6A9955\"> Determine script location for Windows Batch File<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">REM<\/span><span style=\"color: #6A9955\"> Get current folder with no trailing slash<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">SET<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">ScriptDir<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #9CDCFE\">%~dp0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">SET<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">ScriptDir<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #9CDCFE\">%ScriptDir:~<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #9CDCFE\">,<\/span><span style=\"color: #B5CEA8\">-1<\/span><span style=\"color: #9CDCFE\">%<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">ECHO<\/span><span style=\"color: #D4D4D4\"> Current script directory is <\/span><span style=\"color: #9CDCFE\">%ScriptDir%<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">VBScript<\/h2>\n\n\n\n<p>VBScript is fairly straightforward the full path of the running script is available in&nbsp;<strong>WScript.ScriptFullName<\/strong> and then you can use the FileSystemObject class to get the parent folder name. Here is the script:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">VB<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>' Determine script location for VBScript\nDim oFSO : Set oFSO = CreateObject(\"Scripting.FileSystemObject\")\nDim sScriptDir : sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)\n\nWscript.Echo \"Current script directory is \" &amp; sScriptDir<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">&#39; Determine script location for VBScript<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">Dim<\/span><span style=\"color: #9CDCFE\"> oFSO <\/span><span style=\"color: #D4D4D4\">:<\/span><span style=\"color: #569CD6\"> Set <\/span><span style=\"color: #9CDCFE\">oFSO<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">CreateObject<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Scripting.FileSystemObject&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">Dim<\/span><span style=\"color: #9CDCFE\"> sScriptDir <\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #9CDCFE\">sScriptDir<\/span><span style=\"color: #D4D4D4\"> = oFSO.<\/span><span style=\"color: #DCDCAA\">GetParentFolderName<\/span><span style=\"color: #D4D4D4\">(WScript.<\/span><span style=\"color: #9CDCFE\">ScriptFullName<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Wscript.Echo <\/span><span style=\"color: #CE9178\">&quot;Current script directory is &quot;<\/span><span style=\"color: #D4D4D4\"> &amp;<\/span><span style=\"color: #9CDCFE\"> sScriptDir<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">PowerShell<\/h2>\n\n\n\n<p>PowerShell users have long used a snippet from <a title=\"PowerShell Archive\" href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2007\/06\/19\/get-scriptdirectory.aspx\" target=\"_blank\" rel=\"noopener\">this post<\/a> that gets the script folder. However, it doesn&#8217;t work as expected depending on how the script was loaded. This altered version should work in all cases:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">PowerShell<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Determine script location for PowerShell\n$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path\n\nWrite-Host \"Current script directory is $ScriptDir\"<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\"># Determine script location for PowerShell<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$ScriptDir<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">Split-Path<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$<\/span><span style=\"color: #569CD6\">script<\/span><span style=\"color: #9CDCFE\">:MyInvocation<\/span><span style=\"color: #DCDCAA\">.MyCommand.Path<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">Write-Host<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;Current script directory is <\/span><span style=\"color: #9CDCFE\">$ScriptDir<\/span><span style=\"color: #CE9178\">&quot;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p>Hopefully this post will be a useful reference to you when trying to remember how to get the current script directory. I know that I&#8217;ll end up referencing it myself in the future!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview This post shows how to quickly get the current script directory using PowerShell, VBScript and Batch &#8211; the most commonly used scripting languages for Windows. The scripts I write usually read in other files or call other scripts. In order for these scripts to run from any location &#8211; such as a UNC path [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1231,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[54,65,55],"class_list":["post-1230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell","tag-scripting","tag-vbscript"],"_links":{"self":[{"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/posts\/1230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/comments?post=1230"}],"version-history":[{"count":11,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/posts\/1230\/revisions"}],"predecessor-version":[{"id":100108,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/posts\/1230\/revisions\/100108"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/media\/1231"}],"wp:attachment":[{"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/media?parent=1230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/categories?post=1230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/tags?post=1230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}