Using shell scripts in windows

Scriptable Shell Objects

The Windows Shell provides a powerful set of automation objects that enable you to program the Shell with Microsoft Visual Basic and scripting languages such as Microsoft JScript (compatible with ECMA 262 language specification) and Microsoft Visual Basic Scripting Edition (VBScript). You can use these objects to access many of the Shell’s features and dialog boxes. For example, you can access the file system, launch programs, and change system settings.

This section introduces the scriptable Shell objects.

  • Shell Versions
  • Instantiating Shell Objects
    • Late Binding
    • HTML OBJECT Element
  • Shell Object
    • Security
  • Folder Objects

Shell Versions

Many of the Shell objects became available in version 4.71 of the Shell. Others are available in version 5.00 and later. Version 5.00 became available with WindowsВ 2000. The following table lists each Shell object under the version of the Shell in which the object became available.

Version 4.71 Version 5.00
Folder DIDiskQuotaUser
FolderItemVerb DiskQuotaControl
FolderItemVerbs Folder2
Shell FolderItem
ShellFolderView FolderItems
ShellUIHelper FolderItems2
ShellWindows IShellDispatch2
WebViewFolderContents IShellLinkDual2
ShellFolderItem
ShellFolderViewOC
ShellLinkObject

Instantiating Shell Objects

To instantiate the Shell objects in Visual Basic applications with early binding, add references to the following libraries in your project:

  • Microsoft Internet Controls (SHDocVw)
  • Microsoft Shell Controls and Automation (Shell32)

Late Binding

You can also instantiate many of the Shell objects with late binding. This approach works in Visual Basic applications and in script. The following example shows how to instantiate the Shell object in JScript.

The following example shows how to instantiate the Folder object in VBScript.

In the preceding example, sDir is the path to the Folder object. Note that the ShellSpecialFolderConstants enumeration values are not available in script.

The ProgID for each of the Shell objects is shown in the following table.

Object ProgID
DIDiskQuotaUser Microsoft.DiskQuota.1
DiskQuotaControl Cannot late bind
Folder shell.Shell_Application.NameSpace(«. «)
Folder2 shell.Shell_Application.NameSpace(«. «)
FolderItem shell.Shell_Application.NameSpace(«. «).Self or Folder.Items.Item or Folder.ParseName
FolderItems Folder.Items
FolderItems2 Folder.Items
FolderItemVerb Shell.NameSpace(«. «).Self.Verbs.Item()
FolderItemVerbs FolderItem.Verbs or Shell.NameSpace(«. «).Self.Verbs
IShellDispatch2 shell.Shell_Application
IShellLinkDual2 Shell.NameSpace(«. «).Self.GetLink or Shell.NameSpace(«. «).Items().GetLink
Shell shell.Shell_Application
ShellFolderItem Shell.NameSpace(«. «).Self or Shell.NameSpace(«. «).Items()
ShellFolderView Cannot late bind
ShellFolderViewOC Cannot late bind
ShellLinkObject Shell.NameSpace(«. «).Self.GetLink or Shell.NameSpace(«. «).Items().GetLink
ShellUIHelper Cannot late bind
ShellWindows shell.Shell_Windows or ShellWindows._NewEnum
WebViewFolderContents Cannot late bind

HTML OBJECT Element

You can also use the OBJECT element to instantiate Shell objects on an HTML page. To do this, set the OBJECT element’s ID attribute to the variable name you will use in your scripts, and identify the object using its registered number (CLASSID). The following HTML creates an instance of the ShellFolderItem object using the OBJECT element.

The following table lists each Shell object and its respective CLASSID.

DIDiskQuotaUser 7988B571-EC89-11cf-9C00-00AA00A14F56
DiskQuotaControl 7988B571-EC89-11cf-9C00-00AA00A14F56
Folder BBCBDE60-C3FF-11CE-8350-444553540000
Folder2 f0d2d8ef-3890-11d2-bf8b-00c04fb93661
FolderItem 744129E0-CBE5-11CE-8350-444553540000
FolderItems 744129E0-CBE5-11CE-8350-444553540000
FolderItems2 C94F0AD0-F363-11d2-A327-00C04F8EEC7F
FolderItemVerb 08EC3E00-50B0-11CF-960C-0080C7F4EE85
FolderItemVerbs 1F8352C0-50B0-11CF-960C-0080C7F4EE85
IShellDispatch2 A4C6892C-3BA9-11d2-9DEA-00C04FB16162
IShellLinkDual2 317EE249-F12E-11d2-B1E4-00C04F8EEB3E
Shell 13709620-C279-11CE-A49E-444553540000
ShellFolderItem 2fe352ea-fd1f-11d2-b1f4-00c04f8eeb3e
ShellFolderView 62112AA1-EBE4-11cf-A5FB-0020AFE7292D
ShellFolderViewOC 4a3df050-23bd-11d2-939f-00a0c91eedba
ShellLinkObject 11219420-1768-11d1-95BE-00609797EA4F
ShellUIHelper 64AB4BB7-111E-11D1-8F79-00C04FC2FBE1
ShellWindows 9BA05972-F6A8-11CF-A442-00A0C90A8F39
WebViewFolderContents 1820FED0-473E-11D0-A96C-00C04FD705A2

Shell Object

The Shell object represents the objects in the Shell. You can use the methods exposed by the Shell object to:

  • Open, explore, and browse for folders.
  • Minimize, restore, cascade, or tile open windows.
  • Launch Control Panel applications.
  • Display system dialog boxes.

Users are perhaps most familiar with the commands they access from the Start menu and the taskbar’s shortcut menu. The taskbar’s shortcut menu appears when users right-click the taskbar. The following HTML Application (HTA) produces a start page with buttons that implement many of the Shell object’s methods. Some of these methods implement features on the Start menu and the taskbar’s shortcut menu.

Security

As an application, an HTA runs under a different security model than a webpage. To interact with a webpage that implements the functionality of the Shell objects, users must enable the Initialize and script ActiveX Controls not marked as safe option for the security zone in which they are viewing the page.

Folder Objects

The Folder object represents a Shell folder. You can use the methods exposed by the Folder object to:

  • Get information about a folder.
  • Create subfolders.
  • Copy and move file objects into the folder.

The FolderItem object represents an item in a Shell folder. Its properties enable you to retrieve information about the item. You can use the methods exposed by this object to run an item’s verbs, or to retrieve information about an item’s FolderItemVerbs object.

The FolderItems object represents a collection of items in a Shell folder. Its methods and properties enable you to retrieve information about the collection.

The following Visual Basic example shows the relationship between several of the folder objects and how they can be used together. When the user clicks the command button called cmdGetPath, the program displays a dialog box that enables the user to select a folder from My Computer, where ssfDRIVES is the ShellSpecialFolderConstants enumeration value for My Computer. When the user chooses a folder, the folder’s path is displayed in the text box called txtPath.

In VBScript, this function is slightly different because the ShellSpecialFolderConstants enumeration values are not available in script. The following example shows the VBScript equivalent of the previous example.

In the following JScript example, which is a direct translation of the preceding VBScript example, note how the empty parentheses ‘()’ are used to invoke the Items and Item methods.

How to run .sh or Shell Script file in Windows 10

Shell Scripts or .SH files are like batch files of Windows which can be executed in Linux or Unix. It is possible to run .sh or Shell Script file in Windows 10 using Windows Subsystem for Linux. In this post, we will show you how to run a Shell Script file in Windows 10.

How to run .sh or Shell Script file in Windows 10

Bash is a Unix shell and command language which can run Shell Script files. You do not need to install Ubuntu or any other Linux Distros unless your scripts need the support of the real Linux kernel. We will share both the methods.

  1. Execute Shell Script file using WSL
  2. Execute Shell Script using Ubuntu on Windows 10

1] Execute Shell Script file using WSL

Install WSL or Windows Subsystem for Linux

Go to Settings > Update & Security > For Developers. Check the Developer Mode radio button. And search for “Windows Features”, choose “Turn Windows features on or off”.

Scroll to find WSL, check the box, and then install it. Once done, one has to reboot to finish installing the requested changes. Press Restart now. BASH will be available in the Command Prompt and PowerShell.

Execute Shell Script Files

  1. Open Command Prompt and navigate to the folder where the script file is available.
  2. Type Bash script-filename.sh and hit the enter key.
  3. It will execute the script, and depending on the file, you should see an output.

On a Linux platform, you usually use SH, but here you need to use BASH. That said, BASH in Windows has its limitations, so if you want to execute in a Linux environment, you need to install Ubuntu or anything similar.

2] Execute Shell Script using Ubuntu on Windows 10

Make sure you have Ubuntu or any other Linux distros installed. Ubuntu will mount or make all your Windows directories available under /mnt. So the C drive is available at /mnt/C. So if the desktop will be available at /mnt/c/users/ /desktop.

Now follow these steps

  1. Type Bash in run prompt, and it will launch the distro prompt.
  2. Navigate to the folder using “cd” command to the folder where the scripts are available.
  3. Type “sh script.sh” and hit enter.

It will execute the script, and if they have a dependency on any of core Linux features.

Since Linux is now available in Windows, you need not use any third party applications like Cygwin. WSL should be enough for most of the scenarios to help you run a shell script in Windows 10.

Date: July 15, 2019 Tags: WSL

Windows Subsystem for Linux has no installed distributions

Failed to Fork error with Ubuntu on Windows 10

WslRegisterDistribution failed with error 0x8007019e and 0x8000000d – WSL

[email protected]

Ashish is a veteran Windows, and Xbox user who excels in writing tips, tricks, and features on it to improve your day to day experience with your devices.

Primary Sidebar

report this ad

Scriptable Shell Objects

The Windows Shell provides a powerful set of automation objects that enable you to program the Shell with Microsoft Visual Basic and scripting languages such as Microsoft JScript (compatible with ECMA 262 language specification) and Microsoft Visual Basic Scripting Edition (VBScript). You can use these objects to access many of the Shell’s features and dialog boxes. For example, you can access the file system, launch programs, and change system settings.

This section introduces the scriptable Shell objects.

Shell Versions

Many of the Shell objects became available in version 4.71 of the Shell. Others are available in version 5.00 and later. Version 5.00 became available with WindowsВ 2000. The following table lists each Shell object under the version of the Shell in which the object became available.

Version 4.71 Version 5.00
Folder DIDiskQuotaUser
FolderItemVerb DiskQuotaControl
FolderItemVerbs Folder2
Shell FolderItem
ShellFolderView FolderItems
ShellUIHelper FolderItems2
ShellWindows IShellDispatch2
WebViewFolderContents IShellLinkDual2
ShellFolderItem
ShellFolderViewOC
ShellLinkObject

Instantiating Shell Objects

To instantiate the Shell objects in Visual Basic applications with early binding, add references to the following libraries in your project:

  • Microsoft Internet Controls (SHDocVw)
  • Microsoft Shell Controls and Automation (Shell32)

Late Binding

You can also instantiate many of the Shell objects with late binding. This approach works in Visual Basic applications and in script. The following example shows how to instantiate the Shell object in JScript.

The following example shows how to instantiate the Folder object in VBScript.

In the preceding example, sDir is the path to the Folder object. Note that the ShellSpecialFolderConstants enumeration values are not available in script.

The ProgID for each of the Shell objects is shown in the following table.

Object ProgID
DIDiskQuotaUser Microsoft.DiskQuota.1
DiskQuotaControl Cannot late bind
Folder shell.Shell_Application.NameSpace(«. «)
Folder2 shell.Shell_Application.NameSpace(«. «)
FolderItem shell.Shell_Application.NameSpace(«. «).Self or Folder.Items.Item or Folder.ParseName
FolderItems Folder.Items
FolderItems2 Folder.Items
FolderItemVerb Shell.NameSpace(«. «).Self.Verbs.Item()
FolderItemVerbs FolderItem.Verbs or Shell.NameSpace(«. «).Self.Verbs
IShellDispatch2 shell.Shell_Application
IShellLinkDual2 Shell.NameSpace(«. «).Self.GetLink or Shell.NameSpace(«. «).Items().GetLink
Shell shell.Shell_Application
ShellFolderItem Shell.NameSpace(«. «).Self or Shell.NameSpace(«. «).Items()
ShellFolderView Cannot late bind
ShellFolderViewOC Cannot late bind
ShellLinkObject Shell.NameSpace(«. «).Self.GetLink or Shell.NameSpace(«. «).Items().GetLink
ShellUIHelper Cannot late bind
ShellWindows shell.Shell_Windows or ShellWindows._NewEnum
WebViewFolderContents Cannot late bind

HTML OBJECT Element

You can also use the OBJECT element to instantiate Shell objects on an HTML page. To do this, set the OBJECT element’s ID attribute to the variable name you will use in your scripts, and identify the object using its registered number (CLASSID). The following HTML creates an instance of the ShellFolderItem object using the OBJECT element.

The following table lists each Shell object and its respective CLASSID.

DIDiskQuotaUser 7988B571-EC89-11cf-9C00-00AA00A14F56
DiskQuotaControl 7988B571-EC89-11cf-9C00-00AA00A14F56
Folder BBCBDE60-C3FF-11CE-8350-444553540000
Folder2 f0d2d8ef-3890-11d2-bf8b-00c04fb93661
FolderItem 744129E0-CBE5-11CE-8350-444553540000
FolderItems 744129E0-CBE5-11CE-8350-444553540000
FolderItems2 C94F0AD0-F363-11d2-A327-00C04F8EEC7F
FolderItemVerb 08EC3E00-50B0-11CF-960C-0080C7F4EE85
FolderItemVerbs 1F8352C0-50B0-11CF-960C-0080C7F4EE85
IShellDispatch2 A4C6892C-3BA9-11d2-9DEA-00C04FB16162
IShellLinkDual2 317EE249-F12E-11d2-B1E4-00C04F8EEB3E
Shell 13709620-C279-11CE-A49E-444553540000
ShellFolderItem 2fe352ea-fd1f-11d2-b1f4-00c04f8eeb3e
ShellFolderView 62112AA1-EBE4-11cf-A5FB-0020AFE7292D
ShellFolderViewOC 4a3df050-23bd-11d2-939f-00a0c91eedba
ShellLinkObject 11219420-1768-11d1-95BE-00609797EA4F
ShellUIHelper 64AB4BB7-111E-11D1-8F79-00C04FC2FBE1
ShellWindows 9BA05972-F6A8-11CF-A442-00A0C90A8F39
WebViewFolderContents 1820FED0-473E-11D0-A96C-00C04FD705A2

Shell Object

The Shell object represents the objects in the Shell. You can use the methods exposed by the Shell object to:

  • Open, explore, and browse for folders.
  • Minimize, restore, cascade, or tile open windows.
  • Launch Control Panel applications.
  • Display system dialog boxes.

Users are perhaps most familiar with the commands they access from the Start menu and the taskbar’s shortcut menu. The taskbar’s shortcut menu appears when users right-click the taskbar. The following HTML Application (HTA) produces a start page with buttons that implement many of the Shell object’s methods. Some of these methods implement features on the Start menu and the taskbar’s shortcut menu.

Security

As an application, an HTA runs under a different security model than a webpage. To interact with a webpage that implements the functionality of the Shell objects, users must enable the Initialize and script ActiveX Controls not marked as safe option for the security zone in which they are viewing the page.

Folder Objects

The Folder object represents a Shell folder. You can use the methods exposed by the Folder object to:

  • Get information about a folder.
  • Create subfolders.
  • Copy and move file objects into the folder.

The FolderItem object represents an item in a Shell folder. Its properties enable you to retrieve information about the item. You can use the methods exposed by this object to run an item’s verbs, or to retrieve information about an item’s FolderItemVerbs object.

The FolderItems object represents a collection of items in a Shell folder. Its methods and properties enable you to retrieve information about the collection.

The following Visual Basic example shows the relationship between several of the folder objects and how they can be used together. When the user clicks the command button called cmdGetPath, the program displays a dialog box that enables the user to select a folder from My Computer, where ssfDRIVES is the ShellSpecialFolderConstants enumeration value for My Computer. When the user chooses a folder, the folder’s path is displayed in the text box called txtPath.

In VBScript, this function is slightly different because the ShellSpecialFolderConstants enumeration values are not available in script. The following example shows the VBScript equivalent of the previous example.

In the following JScript example, which is a direct translation of the preceding VBScript example, note how the empty parentheses ‘()’ are used to invoke the Items and Item methods.

Читайте также:  Linux узнать занят порт или нет
Оцените статью