Skip to content
⌘ NSIS Forum Archive

Hello world program with PowerShell

14 posts

Chaitanyansis#

Hello world program with PowerShell

Can some one help me in writing a hello world program with PowerShell and execute it using NSIS. I have gone through this link but I am not able to understand clearly what needs to be done. http://nsis.sourceforge.net/PowerShell_support

I already have bunch of PowerShell scripts written, I would like to execute them too.
th_mi#
You need to call the powershell executable with the scritpname as parameter.

exec "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file d:\mydir\myscript.ps1"

But you should keep in mind on a 64 bit system there is a 32 bit and a 64 bit powershell installed. It may make a difference what powershell you are using depending on what the script is doing and if you are also using snapins, ....
Chaitanyansis#
Originally Posted by th_mi View Post
You need to call the powershell executable with the scritpname as parameter.

exec "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file d:\mydir\myscript.ps1"

But you should keep in mind on a 64 bit system there is a 32 bit and a 64 bit powershell installed. It may make a difference what powershell you are using depending on what the script is doing and if you are also using snapins, ....
Hi thanks for the post I have done as following which is working fine

!include "x64.nsh"
Name "nsExec Test"
OutFile "nsExecTest.exe"
#ShowInstDetails show
Section "Output to variable"
    nsExec::ExecToStack 'powershell.exe "& "Import-Module C:\Dorababu\PowerShell\Hello.psm1"'
    Pop $0
    Pop $1
    DetailPrint '"ImportModules" printed: $1'
SectionEnd 
But the same I would like to achieve which is accepting parameters inside PS module script can you help me on that

This is my PowerShell module script

# Filename: TestParameter.psm1
function TestParameter([string] $TestParam)
{
    Write-Host
    Write-Host '$TestParam'
    Write-Host "Good-bye $TestParam! `n"
}
# end of script 
Chaitanyansis#
Originally Posted by jpderuiter View Post
I know how to pass the variables if it is a direct script but how can I achieve it using NSIS nsExec::ExecToStack
th_mi#
Hello Chaitanyansis

In your case I'd try the followerd. I only explain how it could work, not the NSIS stuff.

Assuming you have the followed ps1 script (do not use in this case psm1 since you need to use import-module. If you have a psm1 you shoudl handle this in the ps1 script you call.

Script test.ps1:
Param([Parameter(Mandatory=$False)][string]$param1="",
[Parameter(Mandatory=$False)][string]$param2="")

Write-host "Parameter: $param1 $param2"

How to call the script from normal command prompt:
powershell -file test.ps1 -param1 1 -param2 2

The result would be:
Parameter: 1 2
Chaitanyansis#
Hello th_mi,

I know how to execute a PowerShell script from command prompt but the thing is I have a requirement as follows. I will create a nsi dialog with some controls like text-boxes where user will enter value and submit so that on submit I need to execute the PowerShell script
th_mi#
exec "powershell.exe -file test.ps1 -param1 1 -param2 2"

Maybe you need the path to the ps1 file....!?
th_mi#
Originally Posted by Chaitanyansis View Post
Didn't worked

EXEC 'powershell.exe "& "C:\PowerShell\TestParameter.ps1 -param1 1"'
I tested it, for me it worked perfectly exactly the way I described. I used the script I posted here and also the exec call. The only thing I did was addeng the path to the powershell.exe and also to the test.ps1.

It worked as expected!
Chaitanyansis#
Originally Posted by th_mi View Post
I tested it, for me it worked perfectly exactly the way I described. I used the script I posted here and also the exec call. The only thing I did was addeng the path to the powershell.exe and also to the test.ps1.

It worked as expected!
Hi th_mi when we are writing with out a function it is working fine but when I create the script as follows it didn't worked for me

function CallMe
{
Param([Parameter(Mandatory=$False)][string]$param1="")

Write-host "Parameter: $param1"
}

This is working fine

Param([Parameter(Mandatory=$False)][string]$param1="")

Write-host "Parameter: $param1"
th_mi#
You asked for a solution I gave one to you. If you don't like my solution it is up to you find a better one for you. 🙂
Chaitanyansis#
Originally Posted by th_mi View Post
You asked for a solution I gave one to you. If you don't like my solution it is up to you find a better one for you. 🙂
Hi I am not saying that your solution didn't worked, but initially in my post I posted the code with function, so I am just checking to work it with like the requirement
th_mi#
And from my understanding you will not get it to work in this specific situation with a script that uses the function keyword. But I may be wrong I'm using powershell only for a short time of 3 years, therefore I'm far away from a powershell professional.