PowerShell

Run PowerShell Script

Write a PowerShell script in the Low-Code editor and execute it. The standard text output generated by the script will be written to the output.txt file in the workspace. Please refer to the provided documentation or link for guidance on how to use PowerShell scripts.

# Example:  Retrieve a list of all available commands
Get-Command

# Example: Retrieve a list of all processes on the operating system
Get-Process

ENABLE %VAR%

Document variables in PowerShell scripts are supported in version 2.2.20230225 and later. When the option is turned on, the %FILENAME% string in the PowerShell script will be replaced with the content of [FILENAME].txt in the workspace.

#Replace %content% with the content of content.txt in the workspace, then execute it with PowerShell
Write-Output "%content%"

Precautions

When the %VAR% function is enabled, it's important to avoid using the percentage symbol (%) in script comments (# at the beginning). The Low-Code editor supports sequential execution of commands (Cmdlets), meaning that commands are executed line by line from top to bottom. If you need to implement more advanced flow control, such as using for, foreach, if, switch, while, or until statements, you should save the script as a .ps1 file and then call it from the Low-Code environment:

# myscript.ps1 Print 1~100
for ($i = 1; $i -le 100; $i++) {
    Write-Output $i
}
# PowerShell Low-Code, call the ps1 script file above
./myscript.ps1

In Windows PowerShell, there is a default restriction on running .ps1 script files. To overcome this restriction, you can open Windows PowerShell in Administrator mode (Run as Administrator) on your Windows system and use the Set-ExecutionPolicy command to update the setting to RemoteSigned. Here's how you can do it:

PowerShell Low-Code can be used to automate various Microsoft Office applications, such as running a VBA macro in a specific Excel file:

# Define the path of the Excel file
$filePath = "./excel.xlsm"

# Create an Excel object
$excel = New-Object -ComObject Excel.Application

# Open an Excel workbook
$workbook = $excel.Workbooks.Open($filePath)

# Call a VBA Macro named "VBA_Macro_Name"
$excel.Run("VBA_Macro_Name")

# Close the Excel workbook and release memory
$workbook.Close()
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

--

We are dedicated to improving our content. Please let us know if you come across any errors, including spelling, grammar, or other mistakes, as your feedback is valuable to us! 🤖️⚡️

Last updated