Convert a small PS script into a long line in a .BATch file

Double quote literals must be escaped as \"

@echo off

PowerShell^
  $WindowFunction,$RectangleStruct = Add-Type -MemberDefinition '^
  [DllImport(\"user32.dll\", SetLastError = true)]^
  [return: MarshalAs(UnmanagedType.Bool)]^
  public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);^
  [StructLayout(LayoutKind.Sequential)]^
  public struct RECT^
  {^
     public int Left;^
     public int Top;^
     public int Right;^
     public int Bottom;^
  }^
  ' -Name \"type$([guid]::NewGuid() -replace '-')\" -PassThru;^
  $MyWindowHandle = (Get-Process -Id (^
    Get-WmiObject Win32_Process -Filter \"ProcessId=$PID\"^
  ).ParentProcessId).MainWindowHandle;^
  $WindowRect = New-Object -TypeName $RectangleStruct.FullName;^
  $null = $WindowFunction::GetWindowRect($MyWindowHandle,[ref]$WindowRect);^
  Write-Host $WindowRect.Left $WindowRect.Top $WindowRect.Right $WindowRect.Bottom

Leave a Comment