PowerShellRay

Archive for the ‘Powershell’ Category

First of All what you need to do is to create 2 folders “WindowsPowerShell” and “Modules” like shown below:

“%userprofile%\My Documents\WindowsPowerShell\Modules”

Also if you tried to run

PS C:\Users\Raymond\Desktop> dir env:psmodulepath

you will get result like


Name                           Value
----                           -----
PSModulePath                   C:\Users\Raymond\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPower...

Why I am showing this, To let you know that when we use command Import-Module then you write the name of the module it go and search in the folders that it’s specified in psmodulepath.

After that what you need to do using any script editor but I like Windows PowerShell ISE to write your code and then save the file but .psm1 like “ScheduleTask.psm1”

Create folder inside “%userprofile%\My Documents\WindowsPowerShell\Modules” to be like this “%userprofile%\My Documents\WindowsPowerShell\Modules\ScheduleTask

Last step you need to copy the  the psm1 file and place it inside the folder that is created in the previous step but it’s very important to make sure that the folder and psm1 file are the same name.

 

import-module activedirectory
$ADO = Search-ADAccount -AccountExpired
foreach($object in $ADO)
{
Remove-ADObject -Identity $object.DistinguishedName -Confirm:$false

This Command  get active Directory accounts that are expired


import-module activedirectory
Search-ADAccount -AccountExpired
function hotfixcompare ($computer1, $computer2)
{
$node1 = Get-HotFix -ComputerName $computer1
$node2 = Get-HotFix -ComputerName $computer2
Compare-Object -ReferenceObject $node1 -DifferenceObject $node2  -Property HotFixID 
}
hotfixcompare ex01 lyncse |sort -Property sideindicator

node1 = ex01

node2 = lyncse

Sideindicator == means that the 2 nodes have the same hotfixes

Sideindicator => means that the hotfix is installed on the second node “lyncse”

Sideindicator <= means that the hotfix is installed on the first node “ex01”

 

 

#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
# Generated On: 4/15/2011 9:34 PM
# Author By: Raymond
########################################################################
function listprograms {  
  
Get-WmiObject win32_product -ComputerName $text1.Text | Out-GridView  
  
}  
  
function remoteuninstall {  
  
$app = Get-WmiObject win32_product -ComputerName $text1.Text | Where-Object {$_.name -match $text2.Text}  
$app.uninstall() | Select-Object -Property returnvalue  
}
  

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$label2 = New-Object System.Windows.Forms.Label
$label1 = New-Object System.Windows.Forms.Label
$Text2 = New-Object System.Windows.Forms.RichTextBox
$Text1 = New-Object System.Windows.Forms.RichTextBox
$Button2 = New-Object System.Windows.Forms.Button
$Button1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$Button1_OnClick= 
{
#TODO: Place custom script here
remoteuninstall
}

$handler_label2_Click= 
{
#TODO: Place custom script here

}

$handler_EnterComputerName_TextChanged= 
{
#TODO: Place custom script here

}

$handler_label1_Click= 
{
#TODO: Place custom script here

}

$handler_button2_Click= 
{
#TODO: Place custom script here
listprograms

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,212,208,200)
$form1.Text = "Uninstall program"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 490
$System_Drawing_Size.Height = 219
$form1.ClientSize = $System_Drawing_Size

$label2.TabIndex = 5
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 154
$System_Drawing_Size.Height = 23
$label2.Size = $System_Drawing_Size
$label2.Text = "Enter Application Name"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 271
$System_Drawing_Point.Y = 91
$label2.Location = $System_Drawing_Point
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$label2.Name = "label2"
$label2.add_Click($handler_label2_Click)

$form1.Controls.Add($label2)

$label1.TabIndex = 4
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 141
$System_Drawing_Size.Height = 23
$label1.Size = $System_Drawing_Size
$label1.Text = "Enter Computer Name"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 92
$label1.Location = $System_Drawing_Point
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Name = "label1"
$label1.add_Click($handler_label1_Click)

$form1.Controls.Add($label1)

$Text2.Name = "Text2"
$Text2.Text = ""
$Text2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 271
$System_Drawing_Point.Y = 36
$Text2.Location = $System_Drawing_Point
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 175
$System_Drawing_Size.Height = 29
$Text2.Size = $System_Drawing_Size
$Text2.TabIndex = 3

$form1.Controls.Add($Text2)

$Text1.Name = "Text1"
$Text1.Text = ""
$Text1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 36
$Text1.Location = $System_Drawing_Point
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 175
$System_Drawing_Size.Height = 29
$Text1.Size = $System_Drawing_Size
$Text1.TabIndex = 2
$Text1.add_TextChanged($handler_EnterComputerName_TextChanged)

$form1.Controls.Add($Text1)

$Button2.TabIndex = 1
$Button2.Name = "Button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$Button2.Size = $System_Drawing_Size
$Button2.UseVisualStyleBackColor = $True

$Button2.Text = "List installed programs"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 371
$System_Drawing_Point.Y = 162
$Button2.Location = $System_Drawing_Point
$Button2.DataBindings.DefaultDataSourceUpdateMode = 0
$Button2.add_Click($handler_button2_Click)

$form1.Controls.Add($Button2)

$Button1.TabIndex = 0
$Button1.Name = "Button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$Button1.Size = $System_Drawing_Size
$Button1.UseVisualStyleBackColor = $True

$Button1.Text = "Uninstall"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 162
$Button1.Location = $System_Drawing_Point
$Button1.DataBindings.DefaultDataSourceUpdateMode = 0
$Button1.add_Click($Button1_OnClick)

$form1.Controls.Add($Button1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

This script helps you to uninstall programs using PowerShell as to make it dynamic I made it as function so you can bypass the name of the program that you want to uninstall it

function uninstallapp ($x)
{
$app = Get-WmiObject -Class Win32_Product | Where-Object { 
    $_.Name -match "$x" 
}

$app.Uninstall()

}

Here you can find how to run it

PS C:\Windows\system32> uninstallapp “microsoft lync 2010”

below you will get result like this one

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0

You will find below example how to run PowerShell script from CMD or in windows schedule task

C:\>powershell "& 'c:\scripts\Active Directory\CreateNewUser.ps1'"


%d bloggers like this: