Powershell One Liners #1

I recently decided to put together a couple of one-liners and small script blocks that I have recently compiled which didn’t warrant a blog entry but are useful as a reference point in the future and provide a funny one-liner in the process, this one is courtesy of @RealTimVine

timvine

“A friend of mine always wanted to be run over by a steam train. When it happened, he was chuffed to bits….”

Convert scientific notation using math function

I was recently exporting data where the value was in standard form, which is a way of writing numbers that are too big or small to written in decimal form, using the integer math function this can be converted to a decimal value.

[int]("7.68E+02")

Retrieve total physical memory using CIM_PhysicalMemory class

When retrieving the capacity of physical memory using the CIM_PhysicalMemory class this will return information for each memory module on the host, in order to retrieve a total size we use the Invoke-Expression cmdlet to add each value retrieved from the CIM_PhysicalMemory class and return the capacity in MB.

(Invoke-Expression ((Get-WmiObject -Class CIM_PhysicalMemory).Capacity -join "+")) / 1MB

Retrieve Hyperthreading status using Win32_Processor class 

In order to retrieve the status of hyper threading for a hosts processors, we can use the Win32_Processor class to compare the values ‘NumberOfLogigcalProcessors’ and ‘NumberOfCores’, if these are equal hyper threading is not enabled and will return a ‘False’ statement.

$CPU = Get-WmiObject -Class Win32_Processor 
If ($CPU.NumberOfCores -eq $CPU.NumberOfLogicalProcessors){"False"} Else {"True"}

Convert epoch timestamp to human readable date

I recently wanted to convert output where the timestamp value was in a epoch reference date. This can be converted to a general long time date format by creating a new object of the .NET Framework DateTime object and adding the epoch value to the date January 1, 1970 (midnight UTC/GMT).

$Epoch = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
$Epoch.AddSeconds(("1412750187"))


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s