# Running Powershell and executing a command

**URL:** https://forums.powershell.org/t/running-powershell-and-executing-a-command/21223
**Category:** PowerShell Help
**Created:** [December 24, 2022, 8:51am UTC](https://forums.powershell.org/t/running-powershell-and-executing-a-command/21223 "2022-12-24T08:51:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![The\_Power](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@The\_Power](https://forums.powershell.org/u/The_Power)
#### Post date: [December 24, 2022, 8:51am UTC](https://forums.powershell.org/t/running-powershell-and-executing-a-command/21223/1 "2022-12-24T08:51:57Z")

</div>

Hey!

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!

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [December 24, 2022, 9:50am UTC](https://forums.powershell.org/t/running-powershell-and-executing-a-command/21223/2 "2022-12-24T09:50:14Z")

</div>

The\_Power,  
Welcome to the forum. 👋🏽

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 PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

> [@The\_Power](#):
>
> script that opens the Powershell console as an administrator

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

> **[Get-CimInstance (CimCmdlets) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.3)**
>
> This cmdlet is only available on the Windows platform. The Get-CimInstance cmdlet gets the CIM instances of a class from a CIM server. You can specify either the class name or a query for this cmdlet. This cmdlet returns one or more CIM instance...

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

> [@The\_Power](#):
>
> I need to get the results of these commands quickly

I don’t know what you mean with this.

> [@The\_Power](#):
>
> if it is not possible to make them written in a Powershell window then at least to a .txt file

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:

> **[Everything you wanted to know about PSCustomObject - PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.3)**
>
> PSCustomObject is a simple way to create structured data.

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

[PSCustomObject]@{
    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

> **[Out-File (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-7.3)**
>
> The Out-File cmdlet sends output to a file. It implicitly uses PowerShell's formatting system to write to the file. The file receives the same display representation as the terminal. This means that the output may not be ideal for programmatic...

or even better

> **[Export-Csv (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7.3)**
>
> The Export-CSV cmdlet creates a CSV file of the objects that you submit. Each object is a row that includes a character-separated list of the object's property values. You can use the Export-CSV cmdlet to create spreadsheets and share data with...

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

---

<div class="post-metadata">

### Author: ![The\_Power](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@The\_Power](https://forums.powershell.org/u/The_Power)
#### Post date: [December 24, 2022, 6:15pm UTC](https://forums.powershell.org/t/running-powershell-and-executing-a-command/21223/3 "2022-12-24T18:15:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/running-powershell-and-executing-a-command/21223/4 "2024-05-16T20:16:11Z")

</div>


