How to use a wildcard in powershell parameter that doesn't natively support it? - powershell

Okay, sorry for the probably noobish question.
I've been studying PowerShell for a while now, and have run into something I can't quite figure out how to word correctly for google.
In the most basic sense, here is what I'm trying to do.
Get-Process -id 76*
Now I understand that -id will not handle wildcard * characters.
If I wanted to in theory use
Get-Process -id
and create a wildcard script for this purpose, how would I do this? Do i need to create my own function?
I'd like to add as well that PS says specifically the * is not a usable character for the -Name Parameter, yet I can use this. Is this an error with MS?
Thank you for any advice in advance!

Use a (Where-Object) filter over the Get-Process output.
In this case:
Get-Process | where { $_.Id -like '76*' }
(where is an alias for Where-Object cmdlet.)

Related

Where can I find a list of the PowerShell's atributes?

I was just wondering, it would be cool to know all the atributes used in powershell commands so i can list them all and be more efficient when I have to query something, instead of looking online for the command I need.
I'm probably explaining too bad, I mean this options:
Get-ADUser -Filter "name -like '*ldap*'" -Properties * | select Name,SamAccountName,LastLogonDate,PasswordLastSet,EmailAddress,Enabled
The options that go after the option -Filter... Name, SamAccountName, enabled, etc.
I would be really grateful, thanks in advice ü
To find the options for a command, use Get-Help «command» -detailed or look up the command on-line at Microsoft Docs. To find out what methods and properties an object returned from a command might have, pipe the object to Get-Member.

Using comparison operators on parameters of Powershell cmdlets

I'm trying to use the Get-BrokerDesktop cmdlet,
Like any other Powershell cmdlets I can pass it parameters to filter out the results to my needs. So, I could do something like,
Get-brokerdesktop -RegistrationState Unregistered
Which would return an object that only has Unregistered as its RegistrationState.
How would I go about having the ones that are not Unregistered?
I tried,
Get-brokerdesktop -RegistrationState -ne Unregistered
Which is invalid syntax.
Actually, I just noticed an example at the bottom of the linked documentation...
The trick here is to use -Filter like so,
Get-BrokerDesktop -Filter { RegistrationState -ne 'Unregistered' }
Or even better in this case, as proposed by #TheIncorrigible1,
-Filter 'RegistrationState -ne "Unregistered"'

Get current version number of specified program

I started with this, on a recommendation from a friend
Get-WmiObject win32_product | ft name, version
But then I found this, which gives me pause.
A little research led me to this
wmic product where "Name='Revit 2018'" get name,version
Which works as far as the data gathered. And yes, I am looking for a different program in this example. in any case, once I had good info using WMIC I tried to get the data into a variable so I could get just the version number, but the data formatting is something I have never seen before. I was hoping for a simple solution, like
$object = wmic product where "Name='Revit 2018'" get name,version
$object.version
But only the result is an array with 6 items, and only one seems to be the actual data line, and that's a single line, not two properties. And, I really wonder if an old command line utility is the right answer here. If it really is the best way to do this, is there a trick to converting the raw data to something more, PowerShelly? And if it's not the best way to get this info, what is? Is that scary link real, or is Get-WmiObject win32_product actually safe? And if so, is there a way to filter on a specific name, to speed things up? And indeed, Get-WmiObject doesn't work as I was expecting, as
$object = Get-WmiObject win32_product | ft name, version
foreach ($item in $object) {
Write-Host "$($item.version)"
}
Doesn't work as expected at all.
EDIT: This seems to be working as expected, which is progress.
$version = (Get-WmiObject win32_product -filter:"Name = 'Revit 2018'" | Select-Object -property:*).version
Write-Host "$version!"
I guess the question is really, is this a safe and consistent approach, or is there a better one?
Why not use the registry?
Set-Location HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
$app = Get-ChildItem | Where-Object { $_.GetValue("DisplayName") -match 'YourSoftware' }
$app.GetValue("DisplayVersion")
Or
Set-Location HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
$apps = Get-ChildItem
foreach ($app in $apps) {
$app.GetValue("DisplayName","DisplayVersion")
}
Note: You'll also need to check the SysWow64 registry location as well
HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
Note: Not all items will have a display version in which case you always have the option of looking in the installation directory for the executable itself, which should have a version on it.

Most optimal way of retrieving a property in Powershell

I'm just wondering, what is the optimal, fastest way of retrieving a property in powershell?
I'm using the (). right now to identify a remote computer's architecture (x86 or x64):
$arch = (Get-WmiObject –ComputerName XXXXX –Class Win32_OperatingSystem).osarchitecture
sometimes on a very very slow link, the command takes a while to resolve. For this reason, is there a faster way of retrieving a property than the (). method? I know there are different methods, for example:
... | Select-Object -expandproperty osarchitecture
Any suggestion as to which is better amongst all the possibilites? Thank you
Comment from TheIncorrigible1 chosen as answer:
No, there is not a "faster" way to retrieve a powershell object's
property. What is slow is your RPC connection to the PC. As far as the
most supported option? Select-Object -ExpandProperty since the ().
syntax doesn't work on collections before v3 – TheIncorrigible1

Converting a Powershell ADUC query to VBS

So, I have a Powershell script that I use to see if usernames in an array are Smartcard Enabled. A lot of the scripts that are used to automate my company use VBS. Unfortunately my VBS is VERY rusty and I need to convert this powershell into VBS so my lead programmer can use it in a larger script. The script is below. I am leaving out the ADUC Hierarchy for my company's safety. It will be written in the code as "OU=,DC=" Thanks for the assist.
$Array="C:\UserNames.csv"
ForEach($Name in $Array)
{
Get-ADUser -SearchBase "OU=,DC=" -Filter * -Properties * | Where {$_.CN -like "*$Name*"} | Where {$_.SmartcardLogonRequired -eg %False} | Select SamAccountName,GivenName,Surname,SmartcardLogonRequired
}
Turns out he didn't actually want this translated. He needed the UserAccountControl Code for SMARTCARD_REQUIRED (262144). Well, I can scrap the last 3 days of work. Thanks for the comments.