/bundles/itninjaweb/img/Breadcrumb_cap_w.png

Blog Posts tagged with Microsoft PowerShell

Ask a question

HOW TO: Uninstall Program by Name Only

Okay guys, here's something I've been working on. The idea is to pass the below script the name of a program, the script finds the uninstall string and removes it. My idea was this could be used on toolbars, stuff like itunes, etc. Your not gonna want to create a script or distribution for every version (i've done this in the past). Here you would just give it the name of the program you want gone, and if it knows how to uninstall it, it will.

Curerntly supported uninstall strings:

  • Msiexec commands - These are pretty easy
  • Stuff that ends in uninstall.exe - These are usually /S. 
  • Stuff that ends in helper.exe - These are usually /S



Here's the uninstall script. I wrote it in powershell. Save it as "uninstallScript.ps1" :
###########################################
######## Written by DC  2012-02-13#########
###########################################
<#

.SYNOPSIS

Uninstalls software by only passing the Software Title.
Should work with all msiexec string uninstallers. 
For uninstall commands that end in uninstall.exe or helper.exe a "/S" is used as a switch.

.PARAMETER DisplayName

The complete or partial name of the software being uninstalled. Must appear as shown in add / remove programs (case insenstive).


.EXAMPLE

Uninstall-Program Java

Will search the registry and uninstall all instances of Java from a machine.

#>

[cmdletBinding()]
Param
(
[String]$DisplayName = $(throw "DisplayName is Required")
)

Set-Variable -Name ThirtyMachine -Value "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant
Set-Variable -Name SixtyMachine -Value "HKLM:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant
Set-Variable -Name ThirtyUser -Value "HKCU:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant
Set-Variable -Name SixtyUser -Value "HKCU:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant

$regs = $ThirtyMachine,$SixtyMachine,$ThirtyUser,$SixtyUser

foreach ($reg in $regs)
{
if(Test-Path $reg)
{
$SubKeys = Get-ItemProperty "$reg\*"
}
else
{
$SubKeys = $null
}
foreach($key in $SubKeys)
{
if($key.DisplayName -match "$DisplayName")
{

Write-Host "Found Software " $key.DisplayName
if($key.UninstallString -match "^msiexec")
{
$startGUID = $key.UninstallString.IndexOf("{") + 1
$endGuid = $key.UninstallString.IndexOf("}") - $startGUID
$stringer = $key.UninstallString.Substring($startGUID,$endGuid)
Write-Host "Uninstaller Known, now uninstalling"
&msiexec `/qn `/x `{$stringer`}

}
if($key.UninstallString.Replace('"',"") -match 'uninstall.exe\Z' -or $key.UninstallString.replace('"',"") -match 'helper.exe\Z' )
{
$stringer = $key.UninstallString.Replace('"',"")
if(Test-Path $stringer )
{
Write-Host "Possible Uninstaller found. Trying"  $key.UninstallString "/S"
&$stringer /S
}
}
}
}
}


From there you can create a new shell script and upload the ps1 as a dependency. Here's what i have for script text. This example uninstalls all versions of Java. You could try your software there. 
powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file "uninstallScript.ps1" "Java"
Remember to change the script name (at the bottom) from script.sh to script.bat.

Hopefully this helps somebody.

View comments (17)

HOW TO: Find User log On and Log Off Time

Here's something i came up with to find when users are logging on and off of their machines. 

First we need to create a powershell script to find the actual info (i wish i wrote this, found it on the internet):

 

$UserProperty = @{n="User";e={(New-Object System.Security.Principal.SecurityIdentifier $_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}}
$TypeProperty = @{n="Action";e={if($_.EventID -eq 7001) {"Logon"} else {"Logoff"}}}
$TimeProeprty = @{n="Time";e={$_.TimeGenerated}}
Get-EventLog System -Source Microsoft-Windows-Winlogon | select $UserProperty,$TypeProperty,$TimeProeprty

 

Save this as "get_logon_logoff.ps1"

Next create a new script in the K1000. 

Script type = Shell Script.

Upload the ps1 file as a dependency for the script.

Enter this for this script text.

 

IF NOT EXIST C:\windows\tvg (
mkdir c:\windows\tvg
)
powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file get_logon_logoff.ps1 > c:\windows\tvg\log.txt
 

Also make sure you change the script name from script.sh to script.bat.

 

 

 

Run this script on your test machine.

Next to actually see the information we'll create a custom inventory rule.

I called mine "Log on / Log off", but it doesn't really matter.

Whats important is the rule syntax:

ShellCommandTextReturn(cmd.exe /c type c:\windows\tvg\log.txt)


This is what it looks like when we're all done.


I hope this helps and you guys like it!


**post edit, in the script text there should only be one ">" instead of 2 (">>"). I corrected in the code but not the pic.


View comments (10)

Script to copy file

VBScript to copy file:

dim filesys, oShell
Set filesys = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
sup = oShell.ExpandEnvironmentStrings ("%APPDATA%")
des = oShell.ExpandEnvironmentStrings ("%ProgramFiles(x86)%")
destfile= sup & "\AR System\HOME\AR"
sourcefile= des & "\AR System\User\AR"
If filesys.FileExists(sourcefile) Then
   filesys.CopyFile sourcefile, destfile
End If


Powershell Script to copy file:

$SourceFile = "c:\foo\Test.txt";  
$NewFile    = "c:\foo\Test2.txt";  
  
# Now - check to see if $Sourcefile exists, and if so,  
# copy it to $newfile   
if ([System.IO.File]::Exists($SourceFile))  {  
   [System.IO.File]::Copy($SourceFile, $NewFile)  
   "Source File ($SourceFile) copied to ($newFile)"  
}  
else {  
"Source file ($Sourcefile) does not exist."  
}

Be the first to comment

Verify When Windows Password was Changed

In our environment our user account group creates accounts using Novell eDirectory.We currently sync from eDirectory to our domain, but not the other way.We sometimes run into an issue when a user changes their password using their machine instead of a link provided that changes it in eDirectory. This causes issues with other shared sites were passwords are not synced up.

I've seen tools that will do this, but were not supported in Win 7 x64. To assist in troubleshooting I created a powershell script that will prompt for user ID, and it will return when the password was last changed on our domain. This was created from a variety of different Google searches with a few modifications to meet our needs. I thought I would share just in case anyone else has a similar wacky environment.

$user = read-host "Enter User ID"
$searcher=New-Object DirectoryServices.DirectorySearcher
$searcher.Filter="(&(samaccountname=$user))"
$results=$searcher.findone()
$changedtime = [datetime]::fromfiletime($results.properties.pwdlastset[0])
$c = "The user, $user has changed password last time at $changedtime"
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show($c)

 

 

After prompted for User ID this script will return a time stamp of when that password was last changed.

View comments (1)

Get System Model

I have recently changed from an all Dell shop to an all Lenovo shop. I've gotten used to the model under system inventory being the actual model of the machine. With Lenovo I get a combination of seven digits. This is not a huge deal. When searching for drivers I can enter this number and it will find the drivers I need on their site. 

 

To resolve this I created a custom inventory rule with the information below. This will get the system model on Lenovo machines.

ShellCommandTextReturn(powershell Get-WmiObject Win32_ComputerSystemProduct | Select Version)

 

After the machine inventories this will now place a new field under custom inventory.

 

 

 

 

 



 

Be the first to comment
Showing 1 - 5 of 27 results

Top Contributors

Talk About K1000 Ticket Rules