Remove an entry from credential manager for all users on Windows - windows

I am currently implementing a "remove settings" for all users in a Windows uninstaller and came over an issue I am not even sure is possible to solve.
The application stores credential entries for the current user using the CredentialManager (keymgr.dll). Let's call the target of the credential "X". On uninstall all credentials with stored with target "X" should be removed on all users. The uninstaller of course requires administrator privileges but still I find it very difficult to accomplish this.
For the current user that command is generally solved via cmdkey /delete=:X from a command prompt. As far as I know cmdkey.exe /list only helps to list entries for the current user and can't remove local entries from another user.
I have learned that the credentials are stored as OS files under the C:\Users\_user_\AppData\Local\Microsoft\Credentials folder, but I can't know which files are the entries I want to delete and removing all would be dangerous for other applications. Also I assume removing OS files will be dangerous and could have limitations (extra UAC prompt?) as well.
Runas command is the closest shot I got but because it requires the password of the user it becomes very difficult and not something I would want in the uninstaller. I also would need a way to get the username and domain for each user and iterate them.
I would prefer to use either cmd or powershell for this.

Don't want to necro an old post but I needed to do this myself so I figured I'd add this in case anyone else needs it:
cmdkey /list | ForEach-Object{if($_ -like "*Target:*" -and $_ -like "*microsoft*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}
Powershell one liner that will remove any credentials with Microsoft in the string.
Reference:
https://gist.github.com/janikvonrotz/7819990
I ran this and it purged it locally without needing to run as admin (but I am a local admin)

The cmdkey.exe utility when run from a batch file or a PowerShell command may encounter two issues related to special characters.
1. If run from a batch file, if the credential has "(" or ")" without the double quotes, that is left and right paren, that credential will not be removed.
2. If the credential name aka targetname, has a hyphen surronded by spaces the cmdkey will not remove or create a a credential with that string " - ".
There are a few powershell modules written to try and do this, but the only one i found that handles this exceptions was on Github
https://github.com/bamcisnetworks/BAMCIS.CredentialManager
BAMCIS.CredentialManager
Using this i was able to create credentials to set up a test environment with parens or hyphens, but more importantly to remove them by gathering the users list of cached credentials using the modules command and then passing the information in the command to the remove command to remove ALL cached credentials.
One caveat. After removing the command, after some period of time two cached credentials dynamically reappear.
So to address frequent user lock out issues i am going to try and deploy this using SCCM under user context at logoff. Otherwise a system restart after removing the credentials may be needed.
Here is a prototype script that imports the module and then uses it to remove all cached credentials, As always, test, test, test and use at your own risk!
Clear-host
import-Module "$PSScriptRoot\BAMCIS.CredentialManager\BAMCIS.CredentialManager.psd1"
$L = Get-CredManCredentialList -ErrorAction SilentlyContinue
If($L -eq $null)
{
Write-host "No Cached Credentials found to remove, no action taken"
$LASTEXITCODE = 0
Write-host "The last exit code is $LASTEXITCODE"
}
Else
{
ForEach($cred in $L)
{
Write-host "`tProcessing...`n"
Write-host "$($cred.TargetName.ToString())`n"
Write-host "$($cred.Type.ToString())`n`n"
$R = Remove-CredManCredential -TargetName $($cred.TargetName.ToString()) -Type $($cred.Type.ToString()) -Force
}
$L = Get-CredManCredentialList -ErrorAction SilentlyContinue -ErrorVariable $Cred_Error
If($L -eq $null)
{
Write-host "All Cached Credentials removed, program Complete"
$LASTEXITCODE = 0
Write-host "The last exit code is $LASTEXITCODE"
}
Else
{
Write-host "WARNING: One or more Cached Credentials were not removed, program Complete"
$LASTEXITCODE = 1
}
}

Batch, elevated cmd prompt:
To find the main ones, lists any with MS.O, Micro and teams:
for /f "tokens=1-4 Delims=:=" %A in ('cmdkey /list ^| findstr Target: ^| findstr /i "MS.O Micro Teams"') do #echo %D
This deletes all the entries for teams:
for /f "tokens=1-4 Delims=:=" %A in ('cmdkey /list ^| findstr /i teams') do #cmdkey /delete:%D
If you want to include this as a script the syntax will be slightly different. Double the %%A %%D vars

Ran into similar issue, clearing old credentials for non domain joined devices. Created logon task to clear credentials. In my case was looking for particular file server, so not to clear all creds by accident. Because of syntax issue when piping string expressions into task, was easier to create reference .ps1 then reference in task. Pushed this script through Intune....
# Full path of the file
$file = 'C:\script\clearcreds.ps1'
#If the file does not exist, create it.
if (-not(Test-Path -Path $file -PathType Leaf)) {
try {
$null = New-Item -Path "C:\ProgramData\CentraStage\Packages" -Name
"clearcreds.ps1" -ItemType "file" -Value 'cmdkey /list | ForEach-Object{if($_ -
like "*Target:*" -and $_ -like "*fileserver*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}'
Write-Host "The file [$file] has been created."
}
catch {
throw $_.Exception.Message
}
}
#######################################
#Create ScheduledTask to Run at log on.
#######################################
$schtaskName = "Clear Cached Creds "
$schtaskDescription = "Clears Cached Creds for each user at logon."
$trigger = New-ScheduledTaskTrigger -AtLogOn
$class = cimclass MSFT_TaskEventTrigger root/Microsoft/Windows/TaskScheduler
#Execute task in users context
$principal = New-ScheduledTaskPrincipal -GroupId "S-1-5-32-545" -Id "Author"
$action3 = New-ScheduledTaskAction -Execute 'Powershell.exe' "-File C:\script\clearcreds.ps1"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$null=Register-ScheduledTask -TaskName $schtaskName -Trigger $trigger -Action $action3 -Principal $principal -Settings $settings -Description
$schtaskDescription -Force
Start-ScheduledTask -TaskName $schtaskName

Related

Registry Powershell Output

I need your help
I need to print an logic condition in the specifc PowerShell output command.
Get-ItemProperty -Path 'HKLM:SOFTWARE\Policies\Microsoft\WindowsNT\TerminalServices' -Name fDisableCdm | Select fDisableCdm
I wrote a little script for test, but in my script I made a own conditional output. But I want print that what value have inside registry key and not print an own condition.
My script
if ($process=Get-ItemProperty -Path 'HKLM:SOFTWARE\Policies\Microsoft\WindowsNT\TerminalServices' -Name fDisableCdm -ErrorAction SilentlyContinue) {write-host "1"} else {write-host "0"}
You can help me ?
I waiting for help and tanks for all !
$regKey = "HKLM:SOFTWARE\Policies\Microsoft\WindowsNT\Terminal Services"
$regValue = 'fDisableCdm'
Try {
$process=Get-ItemProperty -Path $regKey -Name $regValue -ErrorAction Stop
Write-Output (-join($regValue,": ",$process.$regValue))
}
Catch {
Write-Output "There was an error: $($PSItem.ToString())"
}
You will need to check to see if your registry key for Terminal Services has a space or not. Mine has a space.
Otherwise, this script allows you to set the two variables at the beginning of the script, performs the requested lookup and, if no error occurs, outputs the requested information.
In the event of an error, the script outputs an appropriate error message.
Reference for meaningful error handling:
https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-exceptions?view=powershell-7.1

Is there any method for getting details of all installed apps in a Windows device using shell commands

I need to get all installed applications and its details in a Windows device using shell commands. I tried using
Get-appxpackage
Get-WmiObject
wmic
Apps that were installed manually seems to be missing in the list. Please help by providing a better method.
An alternative can be to query the registry like this for example:
# HKLM - Local Machine
$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
foreach($obj in $InstalledSoftware){write-host $obj.GetValue('DisplayName') -NoNewline; write-host " - " -NoNewline; write-host $obj.GetValue('DisplayVersion')}
# HKCU - Current User
InstalledSoftware = Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
foreach($obj in $InstalledSoftware){write-host $obj.GetValue('DisplayName') -NoNewline; write-host " - " -NoNewline; write-host $obj.GetValue('DisplayVersion')}
Check this page out for more:
https://www.codetwo.com/admins-blog/how-to-check-installed-software-version/
Tip! Browse these locations in the registry manually before you dig in as it will help you see the structure and understand what properties are available. If the information you're seeking is not there, you might just ditch this suggestion.
For Windows 64-bit and 32-bit apps use
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table > C:\ws\apps.txt
the C:\ws\apps.txt need to be adjusted by you, to your output path.
I found the idea here, Social MS

I want to install VB6 on windows 10 OS. I need to work on VBP project so I have to install VB6 seamlessly

I have VB6 enterprise version set up but it is not able to complete its installation.(Hang)
I have tried installer from following link: http://nuke.vbcorner.net/Tools/VisualStudio6Installer/tabid/93/language/it-IT/Default.aspx
The condition for above installation you need to have msdn image files ready which I don't have. How can I get VB6 installed on Windows 10?
I followed the instructions here to create VB 6 and VB6 SP6 installers for Windows 10.
Simple and fast to create, and worked without issue.
This should work for you, but make sure you read through all the comments as the zero-byte file seems to have stopped working on later builds so there are some workarounds others shared. Though honestly, if all it is doing is stopping a reboot, I don't see why that matters. Copied main text here in case link dies some day.
Stop the Reboot
As in my previous tutorials for installing VB6 on Windows 7 and Windows 8, you'll want to create a zero byte file called MSJAVA.DLL. Creating this file in C:\Windows will prevent the need for a reboot at the end of the installation. If you're not familiar with how to create a zero-byte file, just click that link back there.
Let's Get Started
The first thing that you'll notice after inserting your installation media is the Program Compatibility Assistant popping up with a warning of known compatibility issues. Close it - we need to run setup manually. If you have autorun turned off, you'll get this pop up when you run setup.
Navigate to the installation media, and find the setup program. Right click setup.exe, and select Run As Administrator. Very important! Setup needs to do some registry twiddling and register some items with Windows, and won't be able to do it without the necessary permissions.
Simply click the option that reads Run the program without getting help, and the main setup wizard will start.
The first few screens are the usual stuff, things you've seen before:
Welcome Screen - Gives you the opportunity to vew the readme. Just
click Next unless your really want to read it!
EULA - Yep, it's the End User License Agreement. Scroll it, then
signal your acceptance in the appropriate radio button, then click
Next.
Product Number and USer ID - This is where you put in your user name,
company name and product ID. Fill in the fields as you see fit, and
click Next to continue with the wizard.
What to install - Two options here; VB6 Enterprise Edition, or Server
Applications. I am going with the first option
Common Installation folder - I accepted the default for this:
C:\Program Files (x86)\Microsoft Visual Studio\Common
Welcome and Warning - Copyright protection, inability to update files
in use, etc. Click Continue to move on
Visual Basic 6.0 Enterprise Setup - This is where the actual
installation of VB6 begins. Your PID is shown on the screen, and you
are invited to write it down if you have not already. Click Ok to
continue
Main Installation
On the first screen of the ACM Setup, leave the installation folder at the default, and click the Custom option for setup. The next screen will be the options list.
I don't use SourceSafe, so I cleared the checkbox. If you use SourceSafe, then by all means leave it checked for installation.
Very important (editor's note: see comment at the end) - Clear the checkbox for Data Access. If you don't, set up will hang at the end of the installation. Not sure for the real reason, but the theory is that setup is trying to register with Windows on a non-existent service. Clearing the Data Access checkbox stops this registration attempt.
Click Continue to carry on with the installation process. At this point, if you didn't create the empty MSJAVA.DLL file in C:\Windows, you'll get a restart Windows prompt. Go ahead and restart if you need to, I'll wait.
In any event, you'll get an MSDN installation wizard. I decline this, since much more information is available online anyway.
Now you'll get an option Server Setups dialog. If you want to install BackOffice, Visual SourceSafe Server, or SNA server, you have the opportunity at this point. I don't use these items, so I just click Next to blow by it.
Finally, we get to the last screen of the wizard. Un-check the Register Now checkbox, and click Finish.
Getting VB6 Running for the First Time
You can find the new shortcuts in your start menu, under the usual Microsoft Visual Basic 6.0 program group. You might be tempted to just fire it up straight away, and you can. But, you'll receive a nasty error about Automation, Error accessing the registry. You can blow by the error, but you'll just keep getting it every time you fire up VB6, and some data-access related items won't work correctly.
So, to get past this behavior, right-click the Microsoft Visual Basic 6.0 program icon in the start menu group, and select Run As Administrator. Click Yes in the resulting UAC dialog, and VB6 will start normally, presenting the new project wizard.
Ok, the first post-setup task is complete. Now on to the final piece.
Fixing the IDE Chug
Now before you start building a new project or editing an existing one, there is one more bit of configuration you might need to do. In running the IDE in a Windows 10 virtual machine, I've found that the IDE is somewhat sluggish when sizing and/or placing controls on a form, as well as sizing the form itself. This problem also presented itself in Windows 7 and Windows 8. We need to configure a couple things about the run properties of the IDE to fix this.
Be sure the IDE is closed, then right-click the start menu icon again. This time select Open file location.
In the explorer window that appears, right click the Microsoft Visual Basic 6.0 icon, and select properties. In the Properties window, select the Compatibility tab. On the Compatibility tab, click the Change settings for all users button.
In the new VB6 Properties window, place a tick mark in the Run this program in compatibility mode for: checkbox, and select Windows XP (Service Pack 3) from the drop down.
Under Settings, check the Reduced color mode checkbox, and change the dropdown to 16-bit (65536) color.
Put a check mark in the Disable display scaling on high DPI settings.
Click Ok, then Ok again.
Install with Data Access (from last comment as of 7/3/16)
I was succesfully able to install Visual Studio 6 Professional on
windows 10 Pro 64bit WITH Data Access. It is very simple, just
install VS6 as you normally would with Data Access enabled, it will
freeze when you try to finalize the install. Allow it to freeze, then
end the installation task. You will still have all the install files
and will be able to run the program. Now, you will need to install
the VB6 service pack 6, but it won't allow you to since visual studio
did not install correctly. To fix this, install VS6 over again, this
time uncheck data access components, install as normal. Afterward,
run the service pack and you should be good to go.
Update to my above post about the Help facility. Even though I installed the MSDN Library using the CDs and copied an old winhlp32.exe from an XP machine, that allowed me to view ".HLP" files from Win10, but from within VB6 no help worked. Finally, I was able to go to this website and download vshelp.exe.
http://download.cnet.com/Visual-Studio-Help-Engine-for-MSDN-Enables-MSDN-menu-functionality-in-Visual-FoxPro/3000-2213_4-10727794.html
It ran in a flash and made all the Visual Studio / Visual Basic 6.0 Help work including context sensitive help.
The VB6 installer wizard Visual Studio 6.0 Installer wizard to install the VB6 programming IDE and the MSDN library has been downloaded over 175,000 times.
You need to have your VB6 or VS6 CD and your VB6 serial number.
It works on Windows 7, 8.x, or 10 32bit or 64bit.
I got VS6/VB6 Running under Windows 10 by following the many posts on the Internet involving 1) lowering UAC + REGEDIT check, 2) copy a real MSJAVA.DLL from the VS Install Disk 1 IE4 Folder (un-7-Zip MSJAVX86.EXE) dragging MSJAVA.DLL to all the Windows SYSTEM32/SYSWOW64 folders (a zero byte MSJAVA.DLL no longer works), 3) placing and REGSVR32'ing dx7vb.dll (in the same folders as prior step), then using MSCONFIG to boot Win10 in Safe mode, and running the install from the original MSDN CDs. [No CDs? Read on]
For VB6 you only need DISC 1, both MSDN CDs, and the Service Pack 6 (get it on the Internet). WARNING: The install for DISC 1 will become "Not Responding". In my case, after an hour, I figured it must be done, so let Win10 close it as a "not responding window", and it went on with the MSDN and it worked. (you can also install MSDN standalone from the CDs later) If you can't get the SP6 update to work try by putting it on a Thumbdrive in the root and call the Volume name VS6SP6. In fact, for VB6 if you don't have the CDs anymore, you can create CDs with the contents of each install folder provided you give the CD Volume Label names as follows:
VB6 Disc 1 Volume Label: VSP600ENU1
VB6 Disc 3 Volume Label: DN600ENU1
VB6 Disc 4 Volume Label: DN600ENU2
When done, restore MSCONFIG to normal boot then raise your UAC back up.
One Glitch, I've not yet fixed. ".HLP" files are not supported under Windows 10, so no VB6 Help will be available (that's MSDN). However, I've read (but not tried) that I can copy WINHLP32.EXE from an XP machine to Win10 Windows dir. But first I've gotta get my old XP machine running.
For now, I'm re-developing all my VB programs (without the HELP facility) under Windows 10 Home Edition just fine and merrily doing my compiles again!
Hope this helps.
I made this script a while ago because I was having trouble with the installers and fixes I found around the internet. It incorporates all the tricks and tips that I found around the internet into one powershell script.
To run the script you will need to following:
VB6 Installer
Service Pack 6
Mouse Wheel Fix (Optional, set -SkipMouseWheel switch to skip.)
Each of the above should be placed in its own folder. If you save (and then dot-source) the script in a folder that contains these three folders it'll auto-detect everything for you. You can also set your current location in powershell to this folder and copy the script directly to you session and it'll detect everything as well.
Once the script is dot-sourced or pasted in an elevated powershell instance you can run it by calling Install-VB6.
It also has the following parameters if you want to override any default behaviour:
Parameter
Type
Usage
Vb6InstallerPath
String
Path of main VB6 Installer folder
SP6InstallerPath
String
Path of Service Pack 6 Installer folder
SkipMouseWheel
Switch
Skip installing the Mouse Wheel Fixer folder
MouseWheelPath
String
Path of Mouse Wheel fixer. Ignored if -SkipMouseWheel is specified
Regsvr32Path
String
Path to regsvr32.exe. Uses '%SYSTEMROOT%\SysWoW64\regsvr32.exe' if not specified
RegEditPath
String
Path to regedit.exe. '%SYSTEMROOT%\regedit.exe' if not specified
OrganizationName
String
Sets the organization name in the VB6 installer
Notes:
The VB6 and SP6 installer don't like being run from a network drive, so the script will stop if it detects one of the install folders is not on a local drive.
I've only tested this with VB6 Pro, not VB6 Enterprise.
It doesn't install MSDN.
Install-VB6.ps1
#Requires -RunAsAdministrator
#Requires -Version 3
<#
.SYNOPSIS
Installs VB6 to the computer.
.DESCRIPTION
Installs VB6 ide with Service Pack 6 and (optional) Mouse Wheel Fix to the local computer.
.PARAMETER Vb6InstallerPath
The path to the VB6 installer folder.
.PARAMETER SP6InstallerPath
The path the the Service Pack 6 installer folder.
.PARAMETER SkipMouseWheel
Skip installing the Mouse Wheel fix.
.PARAMETER MouseWheelPath
The path the Mouse wheel fix folder.
.PARAMETER Regsvr32Path
The path to RegSvr32.exe
.PARAMETER OrganizationName
The organization name
.PARAMETER RegEditPath
The path to regedit.exe
#>
Function Install-VB6{
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[string]$Vb6InstallerPath,
[Parameter(Mandatory=$false)]
[string]$SP6InstallerPath,
[Parameter(Mandatory=$false)]
[switch]$SkipMouseWheel,
[Parameter(Mandatory=$false)]
[string]$MouseWheelPath,
[Parameter(Mandatory=$false)]
[string]$Regsvr32Path,
[Parameter(Mandatory=$false)]
[string]$OrganizationName,
[Parameter(Mandatory=$false)]
[string]$RegEditPath
)
# Tests if the path is a local path. The installer doesn't like network paths.
function Test-LocalDrive{
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory=$true,
Position=0)]
[string]$Path
)
begin{
$localDrives = Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType<>4" -Property "DeviceID" | Foreach-Object {$_.DeviceID.Replace(":", "")}
}
process{
if(!([bool](Test-Path -Path $Path))){
return $false
}
$item = Get-Item -Path $Path
$drive = $item.PSDrive
if($null -eq $drive){
return $false
}
return ($localDrives -contains $drive.Name)
}
}
function Search-ForFile{
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory=$true,
Position=0)]
[string]$File,
[Parameter(Mandatory=$true,
Position=1)]
[string]$CurrentLocation,
[switch]$IncludeSubDirectory
)
process{
$newPath = $currentLocation
if($IncludeSubDirectory.IsPresent){
$newPath = Join-Path -Path $newPath -ChildPath "*"
}
$newPath = Join-Path -Path $newPath -ChildPath $file
$item = #(Get-Item -Path $newPath)
if($null -eq $item -or $item.Count -eq 0 -or $null -eq $item[0]){
throw ("Could Not find the {0} file." -f $file)
}
return $item[0]
}
}
#region Setting Up File Paths
$currentLocation = $PSScriptRoot
if([System.String]::IsNullOrWhiteSpace($currentLocation)){
$currentLocation = (Get-Location)
}
if([System.String]::IsNullOrWhiteSpace($currentLocation)){
throw "Unable to determine current location"
}
if(!$PSBoundParameters.ContainsKey("Vb6InstallerPath") -or [System.String]::IsNullOrWhiteSpace($Vb6InstallerPath)){
if(!(Test-LocalDrive -Path ($currentLocation))){
Write-Error "The script cannot be ran from a network share."
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
$installerInfo = Search-ForFile -File "SETUP.EXE" -CurrentLocation $currentLocation -IncludeSubDirectory
$installFolder = $installerInfo.DirectoryName
}
else {
if(!(Test-LocalDrive -Path ($Vb6InstallerPath))){
Write-Error "The VB6 Installer Path cannot be a share."
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
$installFolder = $Vb6InstallerPath
$installerInfo = Search-ForFile -File "SETUP.EXE" -CurrentLocation $installFolder
}
$installer2Info = Search-ForFile -File "ACMSETUP.EXE" -CurrentLocation $installFolder -IncludeSubDirectory
$installLocation = $installerInfo.FullName
$install2Location = $installer2Info.FullName
if(!$PSBoundParameters.ContainsKey("SP6InstallerPath") -or [System.String]::IsNullOrWhiteSpace($SP6InstallerPath)){
if(!(Test-LocalDrive -Path ($currentLocation))){
Write-Error "The script cannot be ran from a network share."
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
$SP6Info = Search-ForFile -File "setupsp6.exe" -CurrentLocation $currentLocation -IncludeSubDirectory
$SP6Folder = $SP6Info.DirectoryName
}
else {
if(!(Test-LocalDrive -Path ($SP6InstallerPath))){
Write-Error "The SP6 Installer Path cannot be a network share."
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
$SP6Folder = $SP6InstallerPath
$SP6Info = Search-ForFile -File "setupsp6.exe" -CurrentLocation $SP6Folder
}
$SP6Location = $SP6Info.FullName
if(!$SkipMouseWheel.IsPresent){
if(!$PSBoundParameters.ContainsKey("MouseWheelPath") -or [System.String]::IsNullOrWhiteSpace($MouseWheelPath)){
if(!(Test-LocalDrive -Path ($currentLocation))){
Write-Error "The script cannot be ran from a network share."
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
$MouseWheelDllInfo = Search-ForFile -File "VB6IDEMouseWheelAddin.dll" -CurrentLocation $currentLocation -IncludeSubDirectory
$MouseWheelRegistryInfo = Search-ForFile -File "VBA Mouse Wheel Fix.reg" -CurrentLocation $currentLocation -IncludeSubDirectory
}
else {
if(!(Test-LocalDrive -Path ($SP6InstallerPath))){
Write-Error "The Mouse Wheel Path cannot be a network share."
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
$MouseWheelDllInfo = Search-ForFile -File "VB6IDEMouseWheelAddin.dll" -CurrentLocation $MouseWheelPath
$MouseWheelRegistryInfo = Search-ForFile -File "VBA Mouse Wheel Fix.reg" -CurrentLocation $MouseWheelPath
}
$MouseWheelDll = $MouseWheelDllInfo.FullName
$MouseWheelRegistry = $MouseWheelRegistryInfo.FullName
}
if(!$PSBoundParameters.ContainsKey("Regsvr32Path") -or [System.String]::IsNullOrWhiteSpace($Regsvr32Path)){
$regSvrPath = "$($env:systemroot)\SysWoW64\regsvr32.exe"
}
else{
$regSvrPath = $Regsvr32Path
}
if(!$PSBoundParameters.ContainsKey("RegEditPath") -or [System.String]::IsNullOrWhiteSpace($RegEditPath)){
$RegEditPath = "$($env:systemroot)\regedit.exe"
}
#endregion Setting Up File Paths
#region Test Required Installer Paths Exist
if(!([bool](Test-Path -Path $regSvrPath))){
Write-Error ("Unable to find '{0}'.`r`nThe exe must exist." -f $regSvrPath)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
if(!([bool](Test-Path -Path $RegEditPath))){
Write-Error ("Unable to find '{0}'.`r`nThe exe must exist." -f $RegEditPath)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
if(!$SkipMouseWheel.IsPresent){
if(!([bool](Test-Path -Path $MouseWheelDll))){
Write-Error ("Unable to find '{0}'.`r`nThe 'MouseWheel' Folder must be in the same directory as the install script and the file must exist." -f $MouseWheelDll)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
if(!([bool](Test-Path -Path $MouseWheelRegistry))){
Write-Error ("Unable to find '{0}'.`r`nThe 'MouseWheel' Folder must be in the same directory as the install script and the file must exist." -f $MouseWheelRegistry)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
}
if(!([bool](Test-Path -Path $installFolder))){
Write-Error ("Unable to find '{0}'.`r`nThe 'Installer' Folder must be in the same directory as the install script." -f $installFolder)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
if(!([bool](Test-Path -Path $installLocation))){
Write-Error ("Unable to find '{0}'.`r`nThe 'Installer' Folder must be in the same directory as the install script and the file must exist." -f $installLocation)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
if(!([bool](Test-Path -Path $install2Location))){
Write-Error ("Unable to find '{0}'.`r`nThe 'Installer' Folder must be in the same directory as the install script and the file must exist." -f $install2Location)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
if(!([bool](Test-Path -Path $SP6Location))){
Write-Error ("Unable to find '{0}'.`r`nThe 'SP6' Folder must be in the same directory as the install script and the file must exist." -f $SP6Location)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
return
}
#endregion Test Required Installer Paths Exist
#region Installer Compatibility
# The installer doesn't auto-elevate to run as an administrator.
# We are setting the required keys in the registry to force the installers to run as administrator
# Same as running the 'troubleshoot compatibilty' wizard and selecting the exe's to run as admins.
Write-Host "Setting compatibility mode on setup files."
$layersPath = "REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
if(![bool](Test-Path -Path $layersPath)){
New-Item -Path $layersPath -Force | Out-Null
}
$registryPath = Get-Item -LiteralPath $layersPath
if($null -eq $registryPath.GetValue($installLocation, $null)){
New-ItemProperty -Path $layersPath -Name $installLocation -Value "^ WINXPSP3" -PropertyType "String" -Force | Out-Null
}
if($null -eq $registryPath.GetValue($SP6Location, $null)){
New-ItemProperty -Path $layersPath -Name $SP6Location -Value "^ WINXPSP3" -PropertyType "String" -Force | Out-Null
}
#endregion Installer Compatibility
#region Previous Install Cleanup
# Locations and keys where old vb6 installs can live.
Write-Host "Cleaning up previous install."
$itemsToDelete = #(
"C:\Program Files*\Microsoft Visual Studio\Common",
"C:\Program Files*\Microsoft Visual Studio\MSDN",
"C:\Program Files*\Microsoft Visual Studio\MSDN98",
"C:\Program Files*\Microsoft Visual Studio\VB98",
"C:\Program Files*\Microsoft Visual Studio\VC98",
"C:\Program Files*\Microsoft Visual Studio\*.HTM",
"C:\Program Files*\Microsoft Visual Studio\*.TXT",
"C:\Program Files*\Common Files\Microsoft Shared\MSDesigners98",
"C:\Program Files*\Common Files\Microsoft Shared\MSDN",
"C:\Program Files*\Common Files\Microsoft Shared\VS98",
"C:\Program Files*\Common Files\Microsoft Shared\Wizards98",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\DevStudio",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\HTML Help Collections",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\MSVSDG",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\Visual Basic\6.0",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\Visual Component Manager",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\Visual Modeler",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\6.0",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\DevStudio",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\HTML Help Collections",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\MSVSDG",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Visual Basic\6.0",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Visual Component Manager",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Visual Modeler",
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\VisualStudio\6.0",
"REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\DevStudio",
"REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\MSVSDG",
"REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Visual Basic\6.0",
"REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Visual Modeler",
"REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\VisualFoxPro",
"REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\6.0"
)
$itemsToDelete | Where-Object { Test-Path -Path $_ } | Remove-Item -Force -Recurse | Out-Null
#endregion Previous Install Cleanup
#region Installer Registry Permissions
# The installer needs to be able to write to 'HKEY_CLASSES_ROOT\RDSServer.DataFactory\Clsid'
# but since the installer isn't built for windows and we have to force it to run as an administrator
# we have to give explicit permissions for your computers Administrators group to write to this key (and all its children)
Write-Host "Setting required permissions for installing user on registry."
$registryPermissionPath = "REGISTRY::HKEY_CLASSES_ROOT\RDSServer.DataFactory\Clsid"
Write-Host "`tSetting Up required information."
$acl = Get-ACL -Path $registryPermissionPath
$oldOwner = [System.Security.Principal.NTAccount]::new($acl.Owner)
$administratorIdentity = [System.Security.Principal.NTAccount]::new("Administrators")
Write-Host "`tGiving the script required permissions."
$import = '[DllImport("ntdll.dll")] public static extern int RtlAdjustPrivilege(ulong a, bool b, bool c, ref bool d);'
$ntdll = Add-Type -Member $import -Name NtDll -PassThru
$privileges = #{ SeTakeOwnership = 9; SeBackup = 17; SeRestore = 18 }
foreach ($i in $privileges.Values) {
$null = $ntdll::RtlAdjustPrivilege($i, 1, 0, [ref]0)
}
Write-Host "`tGettting The registry key."
$regKey = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey("RDSServer.DataFactory\Clsid", 'ReadWriteSubTree', 'TakeOwnership')
# We force the Administrators group to be the owner on the key so we can then add required the permissions.
Write-Host "`tSetting Owner."
$acl.SetOwner($administratorIdentity)
$regKey.SetAccessControl($acl)
Write-Host "`tSetting Permission."
$permission = [System.Security.AccessControl.RegistryAccessRule]::new($administratorIdentity, "FullControl", "ContainerInherit", "InheritOnly", "Allow")
$acl.AddAccessRule($permission)
$permission2 = [System.Security.AccessControl.RegistryAccessRule]::new($administratorIdentity, "FullControl", "Allow")
$acl.AddAccessRule($permission2)
$regKey.SetAccessControl($acl)
# Reset the owner to clean-up
Write-Host "`tResetting Owner."
$acl.SetOwner($oldOwner)
$regKey.SetAccessControl($acl)
#endregion Installer Registry Permissions
#region Install
Write-Host "`tStarting Install."
$tempPath = [System.IO.Path]::GetTempPath()
$tempFolder = Join-Path -Path $tempPath -ChildPath ([System.Guid]::NewGuid().ToString("n"))
New-Item -Path $tempFolder -ItemType Directory -Force | Out-Null
$KeyFile = Join-Path -Path $tempFolder -ChildPath ("{0}.dat" -f [System.Guid]::NewGuid().ToString("n"))
$keyFileText = #"
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\6.0\Setup\Microsoft Visual Basic\SetupWizard]
"aspo"=dword:00000000
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\6.0\Setup\Microsoft Visual Basic\SetupWizard]
"aspo"=dword:00000000
"#
$keyFileText | Set-Content -Path $keyFile -Force
& $RegEditPath /S $KeyFile
[string[]]$installerArguments = ("/T", "VB98PRO.stf", "/S", $installFolder, "/n", ($env:USERNAME), "/k", "0000000000", "/b", "1", "/qn1")
if($PSBoundParameters.ContainsKey("OrganizationName") -and ![System.String]::IsNullOrWhiteSpace($OrganizationName)){
$installerArguments += "/o"
$installerArguments += $OrganizationName
}
Start-Process -FilePath $install2Location -wait -NoNewWindow -ArgumentList $installerArguments
Start-Process -FilePath $SP6Location -wait -NoNewWindow -ArgumentList ("/qn1")
Write-Host "Setting Vb6 Compatibility"
$vb6ExeLocations = #(Get-Item -Path "C:\Program Files*\Microsoft Visual Studio\VB98\VB6.EXE" | Select-Object -ExpandProperty FullName)
$registryPath = Get-Item -LiteralPath $layersPath
foreach($vb6ExeLocation in $vb6ExeLocations){
if($null -eq $registryPath.GetValue($vb6ExeLocation, $null)){
New-ItemProperty -Path $layersPath -Name $vb6ExeLocation -Value "^ WINXPSP3" -PropertyType "String" -Force | Out-Null
}
}
if(!$SkipMouseWheel.IsPresent){
Write-Host "Installing Mouse Wheel"
& $regSvrPath /s $MouseWheelDll
& $RegEditPath /S $MouseWheelRegistry
$registryHeaderText = #"
Windows Registry Editor Version 5.00
"#
$registryItemFormat = #"
[{0}\SOFTWARE\Microsoft\Visual Basic\6.0\Addins\VB6IDEMouseWheelAddin.Connect]
"FriendlyName"="MouseWheel Fix"
"LoadBehavior"=dword:00000003
"CommandLineSafe"=dword:00000000
"#
$users = Get-ChildItem -Path "REGISTRY::HKEY_USERS" | Where-Object {$_.Name -notlike "*_Classes"} | Select-Object -ExpandProperty Name
$content = $registryHeaderText
# Install for every user.
foreach($user in $users){
$content += ($registryItemFormat -f $user)
}
$MouseWheelApplyRegistry = Join-Path -Path $tempFolder -ChildPath ("{0}.reg" -f [System.Guid]::NewGuid().ToString("n"))
$content | Set-Content -Path $MouseWheelApplyRegistry -Force
Start-Process $RegEditPath -wait -NoNewWindow -ArgumentList ("/S", $MouseWheelApplyRegistry)
Write-Host "You will still need to enable Mouse Wheel fix in the VB6 IDE." -BackgroundColor Black -ForegroundColor Red
Write-Host "Open a Visual Basic project and go to 'Add-Ins' -> 'Add-In Manager...' " -BackgroundColor Black -ForegroundColor Red
Write-Host "Select 'MouseWheel Fix' and click 'Loaded/Unloaded' and 'Load on Startup'" -BackgroundColor Black -ForegroundColor Red
}
Remove-Item -Path $tempFolder -Force -Recurse | Out-Null
#endregion Install
Write-Host "Install Complete"
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}
Microsoft's support statement for the VB6 programming environment is here...
VB6 support statement

How to change specific registry setting for another user in powershell

Goal:
To edit a specific registry key setting for a specific user, and no others, in powershell.
Known:
OS: Windows 8.1 Embedded Industry Pro (Same as Win 8.1, but with some embedded features)
I can do this manually on the target machine by opening REGEDIT, selecting HKU, then click on File Menu, click on Load Hive, navigate to the user's profile directory, e.g: c:\users\MrEd and when prompted, type in 'ntuser.dat' - import HKEY_CURRENT_USER. The Hive will be loaded into HKU where you can navigate and make necessary modifications.
Summary:
I have a powershell script that returns the SID of the specific user, but when used in context of the registry hive, the hive"is not found" -- so I'm guessing I must be missing a step? How does one "Load Hive" from Powershell? Or am I missing a special, magical, goats-entrails-on-keyboard incantation somewhere?
Param(
[string]$user= $null
)
Function GetSIDfromAcctName()
{
Param(
[Parameter(mandatory=$true)]$userName
)
$myacct = Get-WmiObject Win32_UserAccount -filter "Name='$userName'"
return $myacct.sid
}
if($user)
{
$sid = GetSIDfromAcctName $user
New-PSDrive HKU Registry HKEY_USERS
$myHiveEntry = Get-Item "HKU:\${sid}"
Write-Host "Key:[$myHiveEntry]"
}
Your existing code should work for a user whose hive is already loaded (like a currently logged in user), but it makes no attempt to load the hive.
I don't know of a way to make a programmatic call to load a hive, but you can shell out to reg.exe.
This ends up being kind of janky. It seems to have issues unloading the hive if it's in use anywhere, so I've put a bunch of crap in place in this sample to try to get rid of stuff that might be holding it open, but in my tests, it can take quite a while before the reg unload command is successful, hence the whole retry portion in the finally block.
This is super unpolished, I just whipped it up on the spot.
Function GetSIDfromAcctName()
{
Param(
[Parameter(mandatory=$true)]$userName
)
$myacct = Get-WmiObject Win32_UserAccount -filter "Name='$userName'"
return $myacct.sid
}
$user = 'someuser'
$sid = GetSIDfromAcctName -userName $user
$path = Resolve-Path "$env:USERPROFILE\..\$user\NTUSER.DAT"
try {
reg load "HKU\$sid" $path
#New-PSDrive -Name HKUser -PSProvider Registry -Root "HKEY_USERS\$sid"
#Get-ChildItem HKUser:\
Get-ChildItem Registry::\HKEY_USERS\$sid
} finally {
#Remove-PSDrive -Name HKUser
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
$retryCount = 0
$retryLimit = 20
$retryTime = 1 #seconds
reg unload "HKU\$sid" #> $null
while ($LASTEXITCODE -ne 0 -and $retryCount -lt $retryLimit) {
Write-Verbose "Error unloading 'HKU\$sid', waiting and trying again." -Verbose
Start-Sleep -Seconds $retryTime
$retryCount++
reg unload "HKU\$sid"
}
}
This doesn't use a PS drive, but that code is in there too, commented out.
Note that if you don't name the hive mount point with the SID, you won't actually need the SID at all because you use the username to find the NTUSER.DAT file anyway.

Is there a way to check AD group membership for a computer?

I am trying to check computer group membership through Powershell. I want to be able to specify a certain computer name and find which groups that computer is in but from a Powershell script. I am planning on running the script on a computer, grabbing the hostname, and then printing out what AD groups that computer is in. Is there an easy way to do this?
EDIT:
So the plan here is to have a computer check to see what groups it is in, then assign a printer based on which group it is in. We have many printers that only 3 to 4 people use but due to the spread out nature of the users cannot downsize the amount of printers. I was looking at group policy but did not want to create 20 different GPOs. I wanted to do this with a logon/startup script. I'm not sure if this is doable or feasible.
Edit #2:
This edit it really late to the party but I figured if anyone found this it could help. We ended up using item level targeting on User>Preferences>Control Panel>Printers objects. We made an account in AD for each group of users needing access to the printers. This worked although it did create a long logon process for the first logon of the computer. We also enabled Point-to-Print restrictions so the drivers were loaded from the servers quietly.
This will give you the group membership (group names) of the local computer (requires powershell 2.0):
([adsisearcher]"(&(objectCategory=computer)(cn=$env:COMPUTERNAME))").FindOne().Properties.memberof -replace '^CN=([^,]+).+$','$1'
Apologies if I'm a bit late to the party on this but I needed to find a computer's group membership as well. After a lot of trial and error, this worked for me.
Get-ADComputer "mycomp" -Properties MemberOf | %{if ($_.MemberOf -like "*group name*") {Write-Host "found"} }
I noticed that if the string comparison is on a separate line, I needed to do the following
$g=Get-ADGroupMember -Identity "CN=myADgroup,OU=myOU,DC=corp,DC=com" -server corp.com
foreach($mbr in $g) {if($name.MemberOf -like "*mycomp*" -eq $true) {Write-Host "found"}}
Not sure but I imagine that testing the computer might be faster and easier than testing the group depending on the number of members.
the gpresult method seems to be the only way I can find that is accurate. Querying AD is not accurate since the computer may have been added to groups but not rebooted. I am sure someone could condense this but it worked well enough for what I needed to see.
# Get all active domain servers
$staledate = (Get-Date).AddDays(-90)
$computers = Get-ADComputer -Filter {(OperatingSystem -Like "*Server*") -and (Enabled -eq $True) -and (LastLogonDate -ge $staledate) -and (Modified -ge $staledate) -and (PasswordLastSet -ge $staledate) -and (whenChanged -ge $staledate) -and (Name -notlike "PHXVSSA101A")} | Select name -expandproperty name
$computers = $computers | Where {(Resolve-DNSName $_.name -ea 0) -and (Test-Connection -ComputerName $_.Name -Count 1 -ea 0)} | Sort
# Loop through all active domain servers
Foreach ($computer in $computers)
{
# Pull the gpresult for the current server
$Lines = gpresult /s $computer /v /SCOPE COMPUTER
# Initialize arrays
$cgroups = #()
$dgroups = #()
# Out equals false by default
$Out = $False
# Define start and end lines for the section we want
$start = "The computer is a part of the following security groups"
$end = "Resultant Set Of Policies for Computer"
# Loop through the gpresult output looking for the computer security group section
ForEach ($Line In $Lines)
{
If ($Line -match $start) {$Out = $True}
If ($Out -eq $True) {$cgroups += $Line}
If ($Line -match $end) {Break}
}
# Loop through all the gathered groups and check for Active Directory groups
ForEach ($group in $cgroups)
{
Try {
$check = Get-ADgroup $group -ErrorAction Stop
If ($check) {
$dgroups += $group
}
}
Catch {}
}
# Output server name and any Active Directory groups it is in
$computer
$dgroups
# End of computers loop
}
try running gpresult /V and check under "security GROUPS"
You can also try gpresult /scope computer /v under a command prompt (elevated to admin) for more specific results
Heres an LDAP query to find if a computer is in a group recursively:
(((objectClass=computer)(sAMAccountName=COMPUTERNAME$))(memberof:1.2.840.113556.1.4.1941:=DistinguishedNameOfGroup))
More info: http://justanotheritblog.co.uk/2016/01/27/recursively-check-if-a-usercomputer-is-a-member-of-an-ad-group-with-powershell-2-0/
To check the computer's own view of group membership, you can run:
(New-Object System.Security.Principal.WindowsPrincipal("$env:computername$")).IsInRole('Example Group')
True
Taking the computer out of Example Group doesn't affect the output of the above until the computer is rebooted.
Try this DOS Command, this will return all the local groups this computer belong to :
net localgroup

Resources