Pull NT user ID from powershell - powershell

get-wmiobject -class win32_computersystem -computername c73118 | format-table username
Will output something similar to:
username
--------
GHS_NTDOMAIN\amacor
Is it possible to only output the amacor part only?

first, you don't really want FT for this I don't think. Use Select -Expand instead. So doing that we get back the string GHS_NTDOMAIN\amacor. Once you have that, you can do .Split("\") to split it into an array of strings, and [-1] to specify the last string in the array. So it would look like:
(get-wmiobject -class win32_computersystem -computername c73118 | Select -ExpandProperty username).Split("\")[-1]
That will result in:
amacor
Or if you wanted to be a bit more verbose about it, you can do:
$Data = get-wmiobject -class win32_computersystem -computername c73118
$DomainUser = $Data.Username
$UserName = $DomainUser.Split("\")[-1]
Then $UserName = "amacor"
Edit: Updated per Andy Arismendi's excellent suggestion.

Related

How to find memory size on Windows 7 64 bit using powershell

I am trying following command, but it does not display the size:
Get-WmiObject Win32_PhysicalMemory
Please let me know what I am doing wrong here.
Thanks
gwmi win32_computersystem | select totalphysicalmemory
The easiest to read would be:
[Math]::Round(((Get-WmiObject -Class Win32_ComputerSystem | Select -Expand TotalPhysicalMemory)/ 1GB),2)
$WMI_ComputerSystem = Get-WmiObject -computername $Current_System.Computer_Name -class Win32_computersystem
$WMI_ComputerSystem.TotalPhysicalMemory
Is not accurate.
$WMI_PM = Get-WmiObject -computername $Current_System.Computer_Name -class Win32_physicalmemory
Returns the size of each DIMM. You can parse through each of the returned values and add them up for an accurate value.

How do I parse data from a Get-WMIObject query into a string?

I have the following line of code...
get-wmiobject -class win32_computersystem | select-object username
It returns (redacted with placeholders)...
#{username=DOMAIN\jsmith}
What needs to be done to remove the padding and give me a "plain" readout of DOMAIN\jsmith?
For bonus points, how do I parse that value into just jsmith?
You need to expand the property to get the value of username instead of a custom object with the property username. Try
get-wmiobject -class win32_computersystem | select-object -expand username
To get the username only, try:
(get-wmiobject -class win32_computersystem | select-object -expand username).Split("\")[2]
You may need to use [1] instead of [2] at the end depending on your OS. In Windows 8, you need 2, while in Windows 7(and older I think), you need 1.
try this
Get-WmiObject -Class Win32_UserAccount | where -property name -eq jsmith | select Name

PowerShell - Select-Object from Win32_OperatingSystem displays rather oddly

First time poster here, I'm a bit of a beginner and I've been keen to get my PowerShell scripting skills up to scratch and I'm come across something rather confusing...
I've made a script to query a collection of computers and I want to query Win32_OperatingSystem but only extrapolate the Build number so I can populate my PSObject with it. I'm trying to add some If logic so that if the build number is 7601, I can write a message under my OS column.
The problem I'm having is that the BuildNumber values are coming out as #{BuildNumber=7601} instead of 7601 for instance. That, and my If statement is borked.
$Machines = Get-Content .\Computers.txt
Foreach($Machine in $Machines)
{
$sweet = (Get-WmiObject -Class Win32_OperatingSystem -computer $Machine | Select-Object BuildNumber)
$dversion = if ($sweet -eq "#{BuildNumber=7601}") {Yes!} else {"Nooooo!"}
New-Object PSObject -Property #{
ComputerName = $Machine
Sweet = $sweet
OS = $dversion
}
}
The issue is that the Get-WMIObject cmdlet is returning a Hash Table. Then the Select-Object is returning just the BuildNumber section you want, the BuildNumber property and it's value. You need to add the -ExpandProperty parameter to only get the value back, not the name/value pair.
Get-WMIObject -Class Win32_OperatingSystem | Select-Object BuildNumber
Returns
#{BuildNumber=7601}
With ExpandProperty
Get-WMIObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber
Returns
7601
Just another option with a ping test to skip unavailable machines.
Get-Content .\Computers.txt | Where-Object {Test-Connection -ComputerName $_ -Count 1 -Quiet} | Foreach-Object {
$sweet = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_ | Select-Object -ExpandProperty BuildNumber
New-Object PSObject -Property #{
ComputerName = $_.__SERVER
Sweet = $sweet
OS = if ($sweet -eq 7601) {'Yes!'} else {'Nooooo!'}
}
}

Powershell: Foreach Input

I'm stuck on something that I know is really simple but for the life of me can't figure out.
I'm trying to import a .txt file with a list of hostnames, which once retrived, the script returns the amount of totalphysicalmemory installed.
so far I have this:
function get
{
$boxes = Get-Content C:\Temp\hostnamesforRAM.txt
foreach($box in $boxes)
{
Get-WmiObject Win32_ComputerSystem | select TotalPhysicalMemory
}
}
get
however, it just simply returns this:
4151570432
4151570432
4151570432
4151570432
4151570432
Any ideas?
Thanks
You need to pass the current hostname ($box) to the computerName parameter, without it you're getting the value from your own computer.
function get
{
$boxes = Get-Content C:\Temp\hostnamesforRAM.txt
foreach($box in $boxes)
{
Get-WmiObject Win32_ComputerSystem -ComputerName $box | select TotalPhysicalMemory
}
}
get
Another way, more shorter, would be to pass the content of the file to the ComputerName parameter. The ComputerName parameter accepts a collection of names:
Get-WmiObject Win32_ComputerSystem -ComputerName (Get-Content C:\Temp\hostnamesforRAM.txt) |
Select-Object Name,TotalPhysicalMemory
This is TotalPhysicalMemory in bytes, to get special computer info add computername to command Get-WmiObject with proper variable name ($box in your case):
Get-WmiObject -computername $box Win32_ComputerSystem
Try
function get
{
$boxes = Get-Content C:\Temp\hostnamesforRAM.txt
foreach($box in $boxes)
{
"$box -> $((Get-WmiObject Win32_ComputerSystem -computername $box).TotalPhysicalMemory)"
}
}

Powershell - filtering WMIObject processes by more than one name

I am trying to get a list of running processes and filter by two process names - can any one tell me how to get this working?
I've so far got it working and filtering out one process name:
$rn = Get-WMIObject Win32_Process -computer servername `
-credential mydomain\administrator -filter "Name='program1.exe'" |
select -expand path
$lst = Get-Content “C:\path\path2\List.txt”
Compare-Object $lst $rn
What I want it to do is filter two process names but nothing I've tried works. Any ideas?
Here's how to get a complete set of Process objects which match a list of process names you're interested in.
$ProcessNames = #( 'explorer.exe', 'notepad.exe' )
Get-WmiObject Win32_Process -Computer 'localhost' |
Where-Object { $ProcessNames -contains $_.Name } |
Select-Object ProcessID, Name, Path |
Format-Table -AutoSize
This example finds all processes, then filters that list by sending them to a pipeline filter that checks to see if the process name is contained in the list of interesting process names. The main benefit of using the pipeline this way is that you can easily access other attributes (such as ProcessID) of the returned processes.
ProcessID Name Path
--------- ---- ----
5832 explorer.exe C:\Windows\Explorer.EXE
4332 notepad.exe C:\Windows\system32\NOTEPAD.EXE
2732 notepad.exe C:\Windows\system32\notepad.exe
Use WQL operators like OR, AND, LIKE etc:
Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe' OR Name='program2.exe'"
Create an array of the processes you're after:
$processes = #('winword.exe', 'notepad.exe', 'excel.exe') | `
% {
$rn = Get-WMIObject Win32_Process -computer servername -credential mydomain\admin -filter "Name='$_'" | select -expand path
#$lst = Get-Content “C:\path\path2\List.txt”
#Compare-Object $lst $rn
write-host $rn
}
I've commented out your compare so you can see how we are looping through the array clearly.
if I understood well try this:
$rn = Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe OR Name='program2.exe'"
Compare-Object $rn[0].path $rn[1].path # if there are only one instance for process with name program1.exe and program2.exe