How do execute powershell from dos command

Hi Experts,

I've the below powershell script, Print-File.ps1, which I want to execute from dos command as below. It is returning error and I am not able to figure the correct syntax.  Can someone advice? Thx.

powershell.exe -command "& 'E:\Batch Jobs\PowerShell Scripts\Print-File.ps1 -file CIS.pdf' "

function print-file($file) 
{ 
 begin  { 
    function internal-printfile($thefile) 
    { 
        if ($thefile -is [string]) {$filename = $thefile } 
        else { 
                if ($thefile.FullName -is [string] ) { $filename = $THEfile.FullName } 
             } 
        $start = new-object System.Diagnostics.ProcessStartInfo $filename 
        $start.Verb = "print" 
        [System.Diagnostics.Process]::Start($start) 
    } 

if ($file -ne $null) { 
                $filespecified = $true; 
                internal-printfile $file 
            } 
       } 
process{if (!$filespecified) { write-Host process ; internal-printfile $_ } } 

}

Open in new window

LVL 17
NicksonKohAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
It wouldn't make sense to execute it, as it only contains a function definition, and the function is never called. Use
powershell -command ". 'E:\Batch Jobs\PowerShell Scripts\Print-File.ps1'; print-file -file CIS.pdf "

Open in new window

0
NicksonKohAuthor Commented:
Thx Qlemo. It works.

Can explain what does the dot in
       -command ". 'E:\Batch Jobs
means?

Also, how can the powershell script be improved to accept parameters?
Is it add
param([string]$f)
print-file $f
function print-file($file) 
...

Open in new window

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Using a dot in front of a script is called "dot-sourcing", If you just call a script without the dot, the script runs, and all variables and function definitions etc. are dismissed after that (to keep the results "local"). With dot, changes are "exported" to the environment of the calling script.
So, just calling the script would not allow to use the function defined in that script outside of that script.

If you want to use it that way, with a script parameter, it is better to remove the function definition. That is, you replace the script with
param($file) 
{ 
 begin  { 
    function internal-printfile($thefile) 
    { 
        if ($thefile -is [string]) {$filename = $thefile } 
        else { 
                if ($thefile.FullName -is [string] ) { $filename = $THEfile.FullName } 
             } 
        $start = new-object System.Diagnostics.ProcessStartInfo $filename 
        $start.Verb = "print" 
        [System.Diagnostics.Process]::Start($start) 
    } 

if ($file -ne $null) { 
                $filespecified = $true; 
                internal-printfile $file 
            } 
       } 
process{if (!$filespecified) { write-Host process ; internal-printfile $_ } } 

}

Open in new window

and name it like the function, "Print-File.ps1" (as it is already).
Then all you need is to use the script, and supply parameters. No dot-sourceing required.
powershell -command "& 'E:\Batch Jobs\PowerShell Scripts\Print-File.ps1' -file CIS.pdf "

Open in new window

However, you should do that only if do not need to call the function repeatedly, as the script gets compiled each time you call it, while a function is compiled only once and reused.
And, as you can see, we now need to use the ampersand operator, as the script path contains spaces. Without ampersand, the string describing the path would be exactly that - a string.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

NicksonKohAuthor Commented:
Thx Qlemo for explaining.

I tried as explained but when the script does not seem to be doing anything except re-printing the entire code?? I execute the script and it just return :

"param {.... }"
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Sorry for that, one pair of curly brackets were wrong, This is the working code:
param($file) 
begin  { 
    function internal-printfile($thefile) 
    { 
        if ($thefile -is [string]) {$filename = $thefile } 
        else { 
                if ($thefile.FullName -is [string] ) { $filename = $THEfile.FullName } 
             } 
        $start = new-object System.Diagnostics.ProcessStartInfo $filename 
        $start.Verb = "print" 
        [System.Diagnostics.Process]::Start($start) |out-null
    } 

if ($file -ne $null) { 
                $filespecified = $true; 
                internal-printfile $file 
            } 
       } 
process{if (!$filespecified) { write-Host process ; internal-printfile $_ } } 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NicksonKohAuthor Commented:
Thx Qlemo for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.