Skip to content

Instantly share code, notes, and snippets.

@markgodiy
Last active January 13, 2023 23:03
Show Gist options
  • Select an option

  • Save markgodiy/4ff859ee06197d4bb9c2d949eeefd4bf to your computer and use it in GitHub Desktop.

Select an option

Save markgodiy/4ff859ee06197d4bb9c2d949eeefd4bf to your computer and use it in GitHub Desktop.
Write PowerShell code within a windows scripting file (.wsf) script. Useful for PowerShell GUI, and scripts that do not require elevated privileges.
<script language="VBScript">
'<![CDATA[
'// CDATA encapsulation is required for powershell to parse this file correctly as XML
'CreateObject("WScript.Shell").Run "powershell -noexit -windowstyle hidden -command ""([xml](Get-Content '" & WScript.ScriptFullName & "')).job.powershell | Invoke-Expression"""
If CreateObject("WScript.Shell").Run("powershell -noexit -command ""([xml](Get-Content '" & WScript.ScriptFullName & "')).job.powershell.'#cdata-section' | Invoke-Expression""",2, False) Then
MsgBox "Script failed to run."
End If
']]></script><powershell><![CDATA[
#-------------------- BEGIN POWERSHELL CODE --------------------
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ InitialDirectory = [Environment]::GetFolderPath('Desktop') }
[void]$FileBrowser.ShowDialog()
$message = "You chose the file: $($FileBrowser.FileName)"
Invoke-WmiMethod -Class win32_process -ComputerName $env:COMPUTERNAME -Name create -ArgumentList "c:\windows\system32\msg.exe * $message"
Exit
#-------------------- END POWERSHELL CODE --------------------
]]></powershell></job>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment