{"id":17821,"date":"2010-07-08T00:01:00","date_gmt":"2010-07-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/07\/08\/hey-scripting-guy-awwwwindows-powershell-plays-nice-nice-with-vbscript-and-batch\/"},"modified":"2010-07-08T00:01:00","modified_gmt":"2010-07-08T00:01:00","slug":"hey-scripting-guy-awwwwindows-powershell-plays-nice-nice-with-vbscript-and-batch","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-awwwwindows-powershell-plays-nice-nice-with-vbscript-and-batch\/","title":{"rendered":"Hey, Scripting Guy! Awww&#8212;Windows PowerShell Plays Nice-Nice with VBScript and Batch!"},"content":{"rendered":"<p>&nbsp;\n<img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\">\nHey, Scripting Guy! I would like to be able to integrate VBScript scripts into my Windows PowerShell code. I also have some batch files I wish I could use with Windows PowerShell. I do not want to have to rewrite code that works perfectly fine. Is this possible?\n&#8212; SK\n&nbsp;\n<img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">\nHello SK,\nMicrosoft Scripting Guy Ed Wilson here. I have many VBScript scripts that I continue to use on a regular basis. I agree with you: In general, I consider rewriting scripts from one language to another language a waste of time, unless there is a compelling reason to do so. There are several reasons for this. Translating a VBScript script into Windows PowerShell is often more work than writing a new script that leverages Windows PowerShell native commands. If you have a script that works, there is little benefit (other than to learn Windows PowerShell) to performing a rewrite unless you add new features to the script. There are so many new things that I can do with Windows PowerShell that I could not do with VBScript, I have not run out of ideas for new scripts yet. Therefore, I do not have time to go back and translate old scripts.\nOne of my new friends that I met on Twitter is Sean Kearney. Sean was recently made a Microsoft MVP for Windows PowerShell, and he is a moderator for the Official Scripting Guys Forum. He also hung out with Craig and others at Tech&middot;Ed 2010 North America in New Orleans. Let&#8217;s just say that if you harnessed the natural energy in Sean&rsquo;s body, you could power much of the massive New Orleans Convention Center.\nSo I asked Sean for ideas about integrating VBScript and Windows PowerShell. As usual he attacked the problem with exuberance. The following is Sean&rsquo;s reply as part of Guest Blogger Week here on the Hey, Scripting Guy! Blog.\n<a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/july\/hey0708\/hsg-07-08-01.jpg\"><img decoding=\"async\" style=\"max-width: 500px;border: 0px\" alt=\"Photo of inimitable Sean Kearney\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/july\/hey0708\/hsg-07-08-01.jpg\" width=\"384\" height=\"288\"><\/a>\n<strong>A little about Sean<\/strong>: Sean Kearney is a network administrator, a Microsoft Certified Technology Specialist in Windows Server Virtualization and Configuration, a Windows PowerShell MVP, and a Microsoft Certified Systems Engineer. Sean is a devoted and passionate computer enthusiast from the early 80s to the present day, having used just about every microcomputer ever. Sean taught himself computer programming with 65xxmachine code, working with many technologies&#8213;but primarily Microsoft. Sean deals with &ldquo;anything thrown at him,&rdquo; from gnawed keyboards to recovery of Exchange servers to networking setups and isolating the realm of the unknown. Currently, he tests and deploys just about any new Microsoft Technology; he also deals with users in an enterprise class environment. Sean loves Windows PowerShell, Windows 7, and Hyper-V, in that order.<strong> <\/strong>\n<strong><\/strong>\nHi, I&rsquo;m Sean. I&rsquo;m a network administrator and a Tech. I&rsquo;ll bet I&rsquo;ve got a job a lot like yours. We deal every day with users and requests. Pull off the impossible now! We solve problems before they happen. And manage the systems.\nMore importantly, I&rsquo;m a scripter. I automate my work. Consistency is king in my world. If there is a way to automate something (that is, when I&rsquo;m not running about like a gerbil in a wheel), I&rsquo;ll do it.\nMost of all, I arm myself with Windows PowerShell. I use Windows PowerShell because it works with all my systems and technologies. It allows me to leverage what I have that is legacy with what I need to use that&rsquo;s being released today.\nAs I write this after having come back from a huge conference I was able to sit and realize something. There seems to be a misconception among many IT pros and developers about Windows PowerShell: If you want to use Windows PowerShell, you have to drop what you already know in order to learn this new thing.\nHowever, that couldn&rsquo;t be further from the truth. Windows PowerShell is about enabling your environment and administrators. Windows PowerShell is an interactive management shell with scripting abilities. It will still allow you to run your VBScript scripts and will happily pass and receive parameters to them. It will pass and receive data to older applications from cmd.exe.\nIt&rsquo;s not about disruption&mdash;it&rsquo;s all about <i>construction<\/i>. It&rsquo;s another tool for you to use and leverage in whatever way that suits you.\nThere are some things that work far better in Windows PowerShell. Querying and manipulating Active Directory and file systems? Windows PowerShell takes to that work naturally. Anything .NET related? That can be extended right to Windows PowerShell because it leverages the .NET Framework for most of its functions.\nBut there are applications on the market that still run and create automatic scripts in VBScript. Why not? It still works fine. And there are too many functions that run directly from the command prompt (NETSH, FTP, and LDIFDE among many others) in which administrators are fluent.\nWindows PowerShell won me over because of some of the things it could perform on a file system easily (like manipulation of files by date\/time). More importantly, it would still interact with both legacy technologies without any loss of functionality.\n*Blink* *Blink* *Sputter* *Blink*\nThat&rsquo;s right. Before you choke on that carbonated beverage in disbelief, Windows PowerShell will easily interact with legacy technologies.\nFor example, to call up a VBScript normally, I would typically execute:\n<span style=\"background-color: #ebebeb\">CSCRIPT.EXE coolscript.vbs<\/span>\nThere&rsquo;s a familiar task and a familiar statement. Guess what? In Windows PowerShell, to run a VBScript I would execute:\n<span style=\"background-color: #ebebeb\">CSCRIPT.EXE coolscript.vbs<\/span>\nNo change!\nSo if you have a VBScript script that needs a parameter called up&mdash;say something that uses a name from a user&mdash;in the command shell you would have typed:\n<span style=\"background-color: #ebebeb\">CSCRIPT UserScript.VBS USERNAME<\/span>\nIf you&rsquo;re working directly in the Windows PowerShell console, that doesn&rsquo;t change. But if you&rsquo;re incorporating that into a Windows PowerShell script, you need to make one change. Use a Windows PowerShell variable to the pass the information:\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- SampleScript.PS1 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/span>\n<span style=\"background-color: #ebebeb\">$FIRST=&rsquo;Dertie&rsquo; <br \/>$LAST=&rsquo; Eyewitt&rsquo; <br \/>$DISPLAY=$FIRSTNAME+&rsquo; &lsquo;+$LASTNAME <br \/>$USERNAME=$FIRSTNAME.Substring(0,1)+$LASTNAME <br \/>$DOMAIN=&rdquo;@HEADOFFICE.CONTOSO.COM&rdquo; <br \/>$UPN=$FIRST+$LAST+$DOMAIN<\/span>\n<span style=\"background-color: #ebebeb\">NEW-ADUSER $USERNAME -Givenname $FIRST &ndash;Lastname $LAST &ndash;UserPrincipalName $UPN &ndash;DisplayName $DISPLAY<\/span>\n<span style=\"background-color: #ebebeb\">CSCRIPT.EXE NewUserScript.VBS $USERNAME<\/span>\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- SampleScript.PS1 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/span>\nOther than a minor change in syntax, it will work. There is an important rule though. Windows PowerShell passes objects not straight text or numeric information. But as long as the object in question contains data in a format VBScript is expecting, it should run properly.\nOf course, test it before running in production. But you knew that already.\nConversely, you can have VBScript call up Windows PowerShell. Windows PowerShell is just another executable application in Windows. It can accept parameters and data just like any other application. Curious what you can send to it?\nJust execute:\n<span style=\"background-color: #ebebeb\">WINDOWS POWERSHELL.EXE \/?<\/span>\nThis will reveal all of the parameters and basic details on them:\n<span style=\"background-color: #ebebeb\">Windows PowerShell[.exe] [-PSConsoleFile &lt;file&gt; | -Version &lt;version&gt;]<\/span>\n<span style=\"background-color: #ebebeb\">[-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive]<\/span>\n<span style=\"background-color: #ebebeb\">[-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]<\/span>\n<span style=\"background-color: #ebebeb\">[-WindowStyle &lt;style&gt;] [-EncodedCommand &lt;Base64EncodedCommand&gt;]<\/span>\n<span style=\"background-color: #ebebeb\">[-File &lt;filePath&gt; &lt;args&gt;] [-ExecutionPolicy &lt;ExecutionPolicy&gt;]<\/span>\n<span style=\"background-color: #ebebeb\">[-Command { &#8211; | &lt;script-block&gt; [-args &lt;arg-array&gt;]<\/span>\n<span style=\"background-color: #ebebeb\">| &lt;string&gt; [&lt;CommandParameters&gt;] } ]<\/span>\nIn actual fact you&rsquo;ll get a lot more than this, but this is just a quick glance at the parameters it will take.\nFor basic purposes, if you want to launch a Windows PowerShell cmdlet from VBScript, here&rsquo;s how you could do it:\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; SampleScript1.vbs &#8212;&#8212;&#8212;&#8212;&#8212;-<\/span>\n<span style=\"background-color: #ebebeb\">Set WshShell= Wscript.CreateObject(&ldquo;Wscript.Shell&rdquo;)<\/span>\n<span style=\"background-color: #ebebeb\">WshShell.Run &ldquo;C:windowssystem32Windows PowerShellv1.0Windows PowerShell.exe &ndash;command Windows PowerShellStatement1; Windows PowerShellStatement2; Windows PowerShellStatement3&rdquo;<\/span>\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; SampleScript1.vbs &#8212;&#8212;&#8212;&#8212;&#8212;-<\/span><\/p>\n<p class=\"MsoNormal\">Or from within VBScript, you can launch a Windows PowerShell script:\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; SampleScript2.vbs &#8212;&#8212;&#8212;&#8212;&#8212;-<\/span>\n<span style=\"background-color: #ebebeb\">Set WshShell= Wscript.CreateObject(&ldquo;Wscript.Shell&rdquo;)<\/span>\n<span style=\"background-color: #ebebeb\">WshShell.Run &ldquo;C:windowssystem32Windows PowerShellv1.0Windows PowerShell.exe &ndash;file { Windows PowerShellScript.PS1 }&rdquo;<\/span>\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; SampleScript2.vbs &#8212;&#8212;&#8212;&#8212;&#8212;-<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Arial','sans-serif';font-size: 10pt\">Not only can you launch Windows PowerShell cmdlets and scripts from VBScript, but also you can pass parameters to them. It works almost exactly as if in CMD from VBScript you were launching a batch file:<\/p>\n<p><\/span>\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; SampleScript3.vbs &#8212;&#8212;&#8212;&#8212;&#8212;-<\/span>\n<span style=\"background-color: #ebebeb\">Set WshShell= Wscript.CreateObject(&ldquo;Wscript.Shell&rdquo;)<\/span>\n<span style=\"background-color: #ebebeb\">PoshScriptName=&rdquo;C:ScriptsAliceinWonderland.PS1&rdquo;<\/span>\n<span style=\"background-color: #ebebeb\">PoshParameter=&rdquo;MadHatter&rdquo;<\/span>\n<span style=\"background-color: #ebebeb\">Posh= &ldquo;C:windowssystem32Windows PowerShellv1.0Windows PowerShell.exe&rdquo;<\/span>\n<span style=\"background-color: #ebebeb\">PoshLaunch=Posh &amp; &ldquo; &ndash;file {&ldquo; &amp; PoshScriptName &amp; &ldquo; &ldquo; PoshParameter &amp; &ldquo; }&rdquo;<\/span>\n<span style=\"background-color: #ebebeb\">WshShell.Run PoshLaunch<\/span>\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; SampleScript3.vbs &#8212;&#8212;&#8212;&#8212;&#8212;-<\/span>\nAnd just as you would pass parameters to your VBScript, you can insert them into the Windows PowerShell command or script (as long as the information matches what it&rsquo;s expecting). Feel a little better? I thought it was the coolest thing when I found out Microsoft left the technologies able to interact with each other. This is not an &ldquo;undocumented feature.&rdquo; This is <i>by design<\/i>. Awesome.\nNow all of you who are comfortable with using cmd.exe, you&rsquo;re not left out. Windows PowerShell will interact with all of the classics (and not-so-classics) as well.\nFor example, you&rsquo;re trying to do something with Robocopy, which wants information like this:\n<span style=\"background-color: #ebebeb\">ROBOCOPY.EXE C:FOLDERA D:FOLDERB<\/span>\nWell again, in Windows PowerShell this is identical:\n<span style=\"background-color: #ebebeb\">ROBOCOPY.EXE C:FOLDERA D:FOLDERB<\/span>\nBut say you need it in a script? No problem. This follows the same rules as with VBScript. Just pass a Windows PowerShell variable to it. The Windows PowerShell console will do all the work to make that command application feel at home.\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8211; SampleScriptB.PS1 &#8212;&#8212;&#8212;&#8212;-<\/span>\n<span style=\"background-color: #ebebeb\">$FOLDERA=&rsquo;C:FOLDERA&rsquo; <br \/>$FOLDERB=&rsquo;D:FOLDERB&rsquo;<\/span>\n<span style=\"background-color: #ebebeb\">NEW-ITEM $FOLDERB &ndash;type Directory -Force<\/span>\n<span style=\"background-color: #ebebeb\">ROBOCOPY $FOLDERA $FOLDERB<\/span>\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8211; SampleScriptB.PS1 &#8212;&#8212;&#8212;&#8212;-<\/span>\nIt&#8217;s nice to have that as an option. But of course every command-line guru knows they can launch parameters from cmd.exe as well. Pretend (just pretend) you need something from an application that only works in cmd.exe. The ability to launch cmd.exe still exists in Windows PowerShell. Running old legacy applications has not gone away.\nAlthough this is not a very impressive use of Windows PowerShell with command, it does show you can pass parameters to DOS commands and applications from Windows PowerShell.exe:\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8211; SampleScriptC.PS1 &#8212;&#8212;&#8212;&#8212;-<\/span>\n<span style=\"background-color: #ebebeb\">$MESSAGEFORECHO=&rsquo;Hello World, I&rsquo;m using Windows PowerShell&rsquo;<\/span>\n<span style=\"background-color: #ebebeb\">CMD.EXE \/K &ldquo;ECHO $MESSAGEFORECHO&rdquo;<\/span>\n<span style=\"background-color: #ebebeb\">&#8212;&#8212;&#8212;&#8212;&#8211; SampleScriptC.PS1 &#8212;&#8212;&#8212;&#8212;-<\/span>\nSo wonderful, we&rsquo;ve discovered everything works with everything else.\nWhy change? It&rsquo;s the things Windows PowerShell offers you that VBScript can&rsquo;t do easily. Hmm, I should be careful here. VBScript can do a lot. So can the command-line controls. That&rsquo;s why we use them: They meet our needs.\nBut they will eventually be phased out. The newer servers such as Exchange 2010 and Windows Server 2008 R2 live in the land of Windows PowerShell. You&rsquo;ll find there won&rsquo;t be much support for VBScript.\nManaging systems and data from a single line in Windows PowerShell session in part is something it does natively and easily. Controlling systems remotely is just a huge part of its nature.\nAlthough VBScript can do WMI, and WMIC enables us in the command world, you should at least give Windows PowerShell a serious glance. All I need to do in Windows PowerShell to change the service tag on a Dell BIOS:\n<span style=\"background-color: #ebebeb\">GET-WMIOBJECT Win32_bios | Select-Object SerialNumber<\/span>\nOr just type:\n<span style=\"background-color: #ebebeb\">GET-WMIOBJECT &ndash;list | EXPORT-CSV C:WMILIST.CSV<\/span>\nThis lets you search and navigate a CSV file of all my WMI objects.\nIf I need to work with Active Directory and find a list of all users created in the last 30 days, I can type this in a Windows PowerShell prompt in Windows Server 2008 R2.\n<span style=\"background-color: #ebebeb\">$DATE=(GET-DATE).AddDays(-30)<\/span>\n<span style=\"background-color: #ebebeb\">GET-ADUSER &ndash;filter &lsquo;whenCreated -gt $DATE&rsquo;<\/span>\nThese are live and interactive commands I can execute in Windows PowerShell. The key word I have just said is &ldquo;interactive.&rdquo;The command is interactive, but it lacks the programmability of the higher end VBScript script. VBScript can do some beautiful and elegant things, but it&rsquo;s not Interactive like command.\nThis is where Windows PowerShell seizes the day: It has the direct interaction of command with the power of VBScript, yet it has a set of capabilities unique to itself including one-liners, aliases, remoting, and user-customizable modules. More importantly, you can use the newer technology while still leveraging components and scripts you have already written.\nWindows PowerShell is another tool for you to leverage in whatever way you need to use it. It is supported and driven not only by Microsoft, but also by us the community. The people that need the tools are those that shape and change Windows PowerShell.\nSo you can easily integrate that company script you wrote in VBScript with the new Exchange Server 2007 with a batch file that performs a series of tasks.\nWindows PowerShell, VBScript and CMD are not three isolated technologies that won&rsquo;t share. They are not three lone warriors. They are a team.\nHere to seize the LAN and save your day.\nSean <br \/>The Energized Tech <br \/><a href=\"http:\/\/www.PowerShell.ca\">www.PowerShell.ca<\/a>\n&nbsp;\nSK, that is all there is to integrating VBScript, Windows PowerShell, and batch. Guest Blogger Week will continue tomorrow.\nIf you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n&nbsp;\n<strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I would like to be able to integrate VBScript scripts into my Windows PowerShell code. I also have some batch files I wish I could use with Windows PowerShell. I do not want to have to rewrite code that works perfectly fine. Is this possible? &#8212; SK &nbsp; Hello SK, Microsoft [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,56,3,4,154,155,45],"class_list":["post-17821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-vbscript-migration","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I would like to be able to integrate VBScript scripts into my Windows PowerShell code. I also have some batch files I wish I could use with Windows PowerShell. I do not want to have to rewrite code that works perfectly fine. Is this possible? &#8212; SK &nbsp; Hello SK, Microsoft [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17821"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17821\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}