Need some help with RegEx

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Need some help with RegEx

Post by MarvelManiac »

I'd like to make this - This is how our printers show in our registry. So I just want to pull the name of the printer.

,,server.printers.net,NYC0304-SILVER-MFD

Into this

NYC0304-SILVER-MFD
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Need some help with RegEx

Post by jvierra »

What is wrong with this:
Get-Printer | select name
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Need some help with RegEx

Post by MarvelManiac »

For remote computers so I was going to use this code
  1. $script:computer = 'Server1'
  2.  
  3. $script:user = (Get-ChildItem  \\$computer\C$\users | Sort-Object LastWriteTime  | Select-Object -last 1 ).name
  4.  
  5.  
  6. $objUser = New-Object System.Security.Principal.NTAccount("$user")
  7. $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
  8. $script:sid = $strSID.Value
  9.  
  10. $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::Users, $Computer)
  11. $PrinterRegistry = $regKey.OpenSubKey("$sid\Printers\Connections")
  12. $Keys = $PrinterRegistry.GetSubKeyNames()
  13.  
  14. $printers= foreach($key in $keys){
  15.     [pscustomobject]@{
  16.         Printer = $key
  17.     }
  18. }
  19.  
  20. $printers
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Need some help with RegEx

Post by jvierra »

Just grab the last piece
($key -split ',')[-1]
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Need some help with RegEx

Post by MarvelManiac »

  1. foreach ($key in $keys)
  2.         {
  3.             [pscustomobject]@{
  4.                 Printer  = ($key -split ',')[-1]
  5.             }
  6.         }

Works beautifully. Thanks!
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked