Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

From PowerShell Zero To SAP GUI Scripting Hero

To use SAP GUI Scripting inside PowerShell you can find here more information about this possibility. The basis of this approach is Visual Basic Script (VBS) resp. the Microsoft Script Control. On this way it is very easy to use the existing scripts furthermore. Here I have compared the use of .NET invoke methods with the use of MSScriptControl. This was a step forward to .NET, but the disadvantage is that you can’t use the advantages of the Integrated Script Environment (ISE) like code completion.

To walk around this shortcoming I searched for a method to cast the ___COMObject types to correct data types in a namespace of a .NET assembly. I found the method CreateWrapperOfType. All I have to do is to build with the type library importer (TLBIMP) a .NET assembly from the SAPFEWSE.OCX library – thats all. If you don’t habe the possibility to build it at your own, look in your NWBC or SapDtsCOMFramework directory, here you find the assembly Interop.SAPFEWSELib.dll, it is also possible to use this assembly.

Hint: The assembly of the NWBC use the namespace Interop.SAPFEWSELib, the assembly of the Dynamic Test Script framework use only SAPFEWSELib. I find that very awkward. If you use the assembly of the NWBC you must modfy the types below and add Interop. in front of the type names.

A code line with a ___COMObject casting to another type of a SAP GUI Scripting object looks like this:

[SAPFEWSELib.GuiApplication]$Application =

Here I cast the variable $Application from the type System.___COMObject to GuiApplication – our base friend of the SAP GUI Scripting object hierarchy. I think it is easy to understand what happens.

Here now the complete code:




  $SapGuiAuto = [Microsoft.VisualBasic.Interaction]::GetObject("SAPGUI")
  If ($SapGuiAuto -eq $Null) {

  $Application = $SapGuiAuto.GetType().InvokeMember("GetScriptingEngine",
        $null, $SapGuiAuto, $null, $null, $null, $null)
  [SAPFEWSELib.GuiApplication]$Application =
  If ($Application -eq $Null) {

  $Connection = $Application.Children.Item(0)
  [SAPFEWSELib.GuiConnectionClass]$Connection =
  If ($Connection -eq $Null) {

  $Session = $Connection.Children.Item(0)
  [SAPFEWSELib.GuiSession]$Session =
  If ($Session -eq $Null) {

  $Mandt = $Session.FindById("wnd[0]/usr/txtRSYST-MANDT")
  [SAPFEWSELib.GuiTextField]$Mandt =
  $BName = $Session.FindById("wnd[0]/usr/txtRSYST-BNAME")
  [SAPFEWSELib.GuiTextField]$BName =
  $Langu = $Session.FindById("wnd[0]/usr/txtRSYST-LANGU")
  [SAPFEWSELib.GuiTextField]$Langu =
  $BCode = $Session.FindById("wnd[0]/usr/pwdRSYST-BCODE")
  [SAPFEWSELib.GuiPasswordField]$BCode =

  $Mandt.Text = "001"
  $BName.Text = "BCUSER"
  $Langu.Text = "EN"
  $Bcode.Text = "minisap"

  $MainWin = $Session.FindById("wnd[0]")
  [SAPFEWSELib.GuiMainWindow]$MainWin =



This example is equivalent to the examples in the links above. Sure it is more complex as VBScript, but it offers all adavantages of the integration in the .NET environment.


This possibility opens now the gate wide to use SAP GUI Scripting seamlessly in PowerShell and in any other language ot the .NET environment.

Hint after: As far as I know uses the NetWeaver Business Client (NWBC), version 4 and 5, as well as the Dynamic Test Scripts (DTS) framework the same method. SAP dts is a test framework which enables automation of testing SAP applications. Look at US patent US 20120023485 A1 for more information about DTS.

Enjoy it.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.