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
Need some help with RegEx
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.
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.
- MarvelManiac
- Posts: 63
- Last visit: Thu Sep 13, 2018 3:40 pm
Re: Need some help with RegEx
For remote computers so I was going to use this code
- $script:computer = 'Server1'
- $script:user = (Get-ChildItem \\$computer\C$\users | Sort-Object LastWriteTime | Select-Object -last 1 ).name
- $objUser = New-Object System.Security.Principal.NTAccount("$user")
- $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
- $script:sid = $strSID.Value
- $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::Users, $Computer)
- $PrinterRegistry = $regKey.OpenSubKey("$sid\Printers\Connections")
- $Keys = $PrinterRegistry.GetSubKeyNames()
- $printers= foreach($key in $keys){
- [pscustomobject]@{
- Printer = $key
- }
- }
- $printers
- MarvelManiac
- Posts: 63
- Last visit: Thu Sep 13, 2018 3:40 pm
Re: Need some help with RegEx
- foreach ($key in $keys)
- {
- [pscustomobject]@{
- Printer = ($key -split ',')[-1]
- }
- }
Works beautifully. Thanks!