6
@ECHO off

$BIOS= Get-WmiObject -computername "BAHRIATSG2-PC" -Namespace 
root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface

$BIOS.SetBIOSSetting('Setup Password','<utf-16/>TheBIOSPassword','<utf-16/>')

pause

when i save as .bat file and run it does not working otherwise it is working properly in powershell when i entering manually..

marcolz
  • 2,880
  • 2
  • 23
  • 28
Alone
  • 81
  • 1
  • 1
  • 7
  • 5
    Possible duplicate of [How to run a PowerShell script from a batch file](https://stackoverflow.com/questions/19335004/how-to-run-a-powershell-script-from-a-batch-file) – arco444 Sep 06 '17 at 08:20
  • 1
    I'm not sure the duplicate question quite explains it. You need to save your script as a .ps1 file and then you could execute it from the a .bat script as described in the above linked question. – Mark Wragg Sep 06 '17 at 08:45
  • Possible duplicate of [How to execute powershell commands from a batch file?](https://stackoverflow.com/questions/6037146/how-to-execute-powershell-commands-from-a-batch-file) – Abdullah Leghari Sep 06 '17 at 09:00
  • 1
    Of course it will work fine in Powershell. It is powershell code. You can't expect CMD.exe to interpret some other scripting languages code and be able to execute it. That goes for ALL scripting languages. – Squashman Sep 06 '17 at 14:49

4 Answers4

13

Enclose your PowerShell code in,

powershell -Command "& {}"

Remember to separate all statements with ; and to enclose your " with a quoted string, i.e by using ""

powershell -Command "& {$BIOS= Get-WmiObject -computername ""BAHRIATSG2-PC\"" -Namespace root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface; $BIOS.SetBIOSSetting('Setup Password','<utf-16/>TheBIOSPassword','<utf-16/>')}"
Abdullah Leghari
  • 2,332
  • 3
  • 24
  • 40
  • 1
    In case anyone else runs across this, as I did -- It may help to use `foreach-object` instead of the `%` alias when executing a command like this. Otherwise, it may result in an `Expressions are only allowed as the first element of a pipeline` error. – Dustin Halstead Mar 08 '18 at 18:44
  • I am trying to "escape" strings like you said in this command without any luck. can you help me please? `(netsh wlan show profiles) | Select-String "\:(.+)$" | %{$name=$_.Matches.Groups[1].Value.Trim(); $_} | %{(netsh wlan show profile name="$name" key=clear)} | Select-String "Key Content\W+\:(.+)$" | %{$pass=$_.Matches.Groups[1].Value.Trim(); $_} | %{[PSCustomObject]@{ PROFILE_NAME=$name;PASSWORD=$pass }} | Format-Table -AutoSize` – user151496 Nov 14 '22 at 10:37
7

I think this is the simplest and clearest way because it don't requires any superfluous switch; just plain PowerShell code:

@ECHO off

PowerShell ^
   $BIOS= Get-WmiObject -computername \"BAHRIATSG2-PC\" -Namespace;  ^ 
   root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface;  ^
   $BIOS.SetBIOSSetting('Setup Password','<utf-16/>TheBIOSPassword','<utf-16/>')

pause

Just add a ; ^ at end of each line except the last line and escape each quote with backslash.

wovano
  • 4,543
  • 5
  • 22
  • 49
Aacini
  • 65,180
  • 12
  • 72
  • 108
2

Following this thread:
https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/converting-powershell-to-batch


you can convert any PowerShell script into a batch file easily using this PowerShell function:

function Convert-PowerShellToBatch
{
    param
    (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string]
        [Alias("FullName")]
        $Path
    )
 
    process
    {
        $encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
        $newPath = [Io.Path]::ChangeExtension($Path, ".bat")
        "@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
    }
}


To convert all PowerShell scripts inside a directory, simply run the following command:

Get-ChildItem -Path <DIR-PATH> -Filter *.ps1 |
  Convert-PowerShellToBatch

Where is the path to the desired folder. For instance:

Get-ChildItem -Path "C:\path\to\powershell\scripts" -Filter *.ps1 |
  Convert-PowerShellToBatch


To convert a single PowerShell script, simply run this:

Get-ChildItem -Path <FILE-PATH> |
  Convert-PowerShellToBatch

Where is the path to the desired file.

The converted files are located in the source directory. i.e., <FILE-PATH> or <DIR-PATH>.

Elyasaf755
  • 2,239
  • 18
  • 24
1

powershell has its own script file type, extension is .ps1

Save your code to a .ps1 file, and run it from a batch file with:

powershell xxx.ps1