Running Powershell and executing a command


Help me write a simple script that opens the Powershell console as an administrator and alternately executes the following commands:
(Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey

(Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKeyDescription

Get-WmiObject win32_bios | select Serialnumber

I need to get the results of these commands quickly, if it is not possible to make them written in a Powershell window then at least to a .txt file

Thank you!

Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

You don’t need to have administrative rights to obtain these informations.

You should not use Get-WmiObject anymore as it’s deprecated and can be replaced by its successor

To save time and ressources you shoud not run a command twice. Instead save the result in a variable and use this later on.

I don’t know what you mean with this.

That’s totally up to you. You can have both if you like.

In genereal … to make the output look a kind of professional I use a [PSCustomObject].

Here you can read more about:

$SLS = Get-CimInstance -Query 'select * from SoftwareLicensingService'
$BIOS = Get-CimInstance -ClassName CIM_BIOSElement

    SerialNumber                  = $BIOS.SerialNumber
    OriginalProductKey            = $SLS.OA3xOriginalProductKey
    OriginalProductKeyDescription = $SLS.OA3xOriginalProductKeyDescription

This will output the results to the console. If you like to save it to a file you can use

or even better

Please always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them.

Thank you, i will test it at my work :grinning:. But it seems the result can be coped if i create a .exe file :face_with_diagonal_mouth: