Calling powershell script from a batch file

Hello All,

I have a batch script that calls a powershell script. Before calling the script I set the execution policy to unrestricted, but when it gets to the line that calls the batch script i still get the confirmation in the command window: "Do you want to perform this operation" I then have to press Y for the PS script to run and then my batch script finishes.

Does anyone know the setting I need to change in order to suppress the confirmation?

Note: this is Windows 8, see code below

   
set THIS_DIR=%~dp0

powershell Set-ExecutionPolicy unrestricted 

powershell %THIS_DIR%MyScript.ps1 "param1"



January 14th, 2014 1:46pm

I may sound like a jerk but you really want to look at PowerShell.exe /?

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

-PSConsoleFile
    Loads the specified Windows PowerShell console file. To create a console
    file, use Export-Console in Windows PowerShell.

-Version
    Starts the specified version of Windows PowerShell.
    Enter a version number with the parameter, such as "-version 2.0".

-NoLogo
    Hides the copyright banner at startup.

-NoExit
    Does not exit after running startup commands.

-Sta
    Starts the shell using a single-threaded apartment.
    Single-threaded apartment (STA) is the default.

-Mta
    Start the shell using a multithreaded apartment.

-NoProfile
    Does not load the Windows PowerShell profile.

-NonInteractive
    Does not present an interactive prompt to the user.

-InputFormat
    Describes the format of data sent to Windows PowerShell. Valid values are
    "Text" (text strings) or "XML" (serialized CLIXML format).

-OutputFormat
    Determines how output from Windows PowerShell is formatted. Valid values
    are "Text" (text strings) or "XML" (serialized CLIXML format).

-WindowStyle
    Sets the window style to Normal, Minimized, Maximized or Hidden.

-EncodedCommand
    Accepts a base-64-encoded string version of a command. Use this parameter
    to submit commands to Windows PowerShell that require complex quotation
    marks or curly braces.

-File
    Runs the specified script in the local scope ("dot-sourced"), so that the
    functions and variables that the script creates are available in the
    current session. Enter the script file path and any parameters.
    File must be the last parameter in the command, because all characters
    typed after the File parameter name are interpreted
    as the script file path followed by the script parameters.

-ExecutionPolicy
    Sets the default execution policy for the current session and saves it
    in the $env:PSExecutionPolicyPreference environment variable.
    This parameter does not change the Windows PowerShell execution policy
    that is set in the registry.

-Command
    Executes the specified commands (and any parameters) as though they were
    typed at the Windows PowerShell command prompt, and then exits, unless
    NoExit is specified. The value of Command can be "-", a string. or a
    script block.

    If the value of Command is "-", the command text is read from standard
    input.

    If the value of Command is a script block, the script block must be enclosed
    in braces ({}). You can specify a script block only when running PowerShell.exe
    in Windows PowerShell. The results of the script block are returned to the
    parent shell as deserialized XML objects, not live objects.

    If the value of Command is a string, Command must be the last parameter
    in the command , because any characters typed after the command are
    interpreted as the command arguments.

    To write a string that runs a Windows PowerShell command, use the format:
        "& {<command>}"
    where the quotation marks indicate a string and the invoke operator (&)
    causes the command to be executed.

-Help, -?, /?
    Shows this message. If you are typing a PowerShell.exe command in Windows
    PowerShell, prepend the command parameters with a hyphen (-), not a forward
    slash (/). You can use either a hyphen or forward slash in Cmd.exe.

Free Windows Admin Tool Kit Click here and download it now
January 14th, 2014 2:27pm

sounds like

PowerShell.exe -NonInteractive

May be the way to go
January 14th, 2014 2:28pm

PowerShell.exe -ExecutionPolicy Bypass -File %THIS_DIR%MyScript.ps1 "param1"

Should do the trick.
Free Windows Admin Tool Kit Click here and download it now
January 14th, 2014 2:30pm

I think that what you are missing, is a "-force"

powershell Set-ExecutionPolicy unrestricted -force

 Let me know if that does the trick

January 14th, 2014 2:33pm

Open Powershell console as an administrator and turn off UAC and then try.

PS:\>powershell Set-ExecutionPolicy unrestricted 

-Prashanth

Free Windows Admin Tool Kit Click here and download it now
January 14th, 2014 3:00pm

Intersting, the -noninteractive seemed to work but inside the powershell script I made a call to Set-WinUserLanguageList. This seems to fail when I am in noninteractive mode. So looks like I may not be able to use this.

Set-WinUserLanguageList : Windows PowerShell is in NonInteractive mode. Read
and Prompt functionality is not available.
At E:\ChangeLanguage.ps1:12 char:1
+ Set-WinUserLanguageList $langlist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Set-WinUserLanguageList],
    PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.InternationalSettings
   .Commands.SetWinUserLanguageListCommand

 
January 14th, 2014 3:06pm

Tried that. Didn't seem to work. I got see above. Thanks for the help
  • Edited by althurm 15 hours 23 minutes ago
Free Windows Admin Tool Kit Click here and download it now
January 14th, 2014 3:06pm

Ahh got it i just had to add the -Force on the call to Set-WinUserLanguageList inside the powershell script. Thanks for the help!

January 14th, 2014 3:11pm

No problem, please mark your question with the appropriate answer/answers.
Free Windows Admin Tool Kit Click here and download it now
January 14th, 2014 3:21pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics