Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Friday, March 25, 2016

PowerShell - Rename Computer in Active Directory

Execute the command below and restart the computer.

Rename-Computer -ComputerName "OLD_COMPUTER_NAME" -NewName "NEW_COMPUTER_NAME" -DomainCredential $cred

Wednesday, September 9, 2015

PowerShell - Uninstall Sophos Antivirus Programs

1. Stop the Sophos AutoUpdate Service to prevent a potential update during the removal process.
2. Gather information for all Sophos applications.
3. Uninstall the applicaions

Enter-PSSession IP_ADDRESS -Credential $cred
net stop "Sophos AutoUpdate Service"
Get-WmiObject -Class win32_product -Filter "Name like '%sophos%'"
$obj = Get-WmiObject -Class win32_product -Filter "Name like '%sophos%'"
$obj.Uninstall()

Monday, May 18, 2015

PowerShell - Detect/Change/Activate Power Plans

Detect Computer power plans
gwmi -NS root\cimv2\power -Class win32_PowerPlan | select ElementName, IsActive | ft -a

Change/Activate power plan
 - Method 1: Using the CIM cmdlets
$powerplan = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = 'High Performance'"
Invoke-CimMethod -InputObject $powerplan -MethodName Activate
$powerplan = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = 'Power Saver'"
Invoke-CimMethod -InputObject $powerplan -MethodName Activate
 - Method 2: Using Get-WmiObject
$powerplan = gwmi -NS root\cimv2\power -Class win32_PowerPlan -Filter "ElementName ='High Performance'"
$powerplan.Activate()
$powerplan = gwmi -NS root\cimv2\power -Class win32_PowerPlan -Filter "ElementName ='Power Saver'"
$powerplan.Activate()

Turn Off Hard Drive Sleep
powercfg -CHANGE -disk-timeout-ac 0

Tuesday, April 7, 2015

PowerShell - Check Hard Drive Size

 Get C: Drive size
$disk = ([wmi]"\root\cimv2:Win32_logicalDisk.DeviceID='C:'")
"C: has {0:#.0} GB free of {1:#.0} GB Total" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output

Monday, March 9, 2015

PowerShell - Print Queue

List Printers
Get-WMIObject -Class Win32_Printer | Select Name,DriverName,PortName,Shared,ShareName | ft -auto

Show Print Queue Status
Get-WMIObject Win32_PerfFormattedData_Spooler_PrintQueue | Select Name, @{Expression={$_.jobs};Label="CurrentJobs"}, TotalJobsPrinted, JobErrors

Delete a print queue (Ex: hp deskjet 960c series)
$p = Get-WmiObject -Class Win32_printer -filter "name='hp deskjet 960c series'"
$p.delete()

Wednesday, September 10, 2014

PowerShell - Change / Mute Audio Volume

Volume Mute Button
$obj = new-object -com wscript.shell
$obj.SendKeys([char]173)

Volume Down Button
$obj = new-object -com wscript.shell
$obj.SendKeys([char]174)

Volume Up Button
$obj = new-object -com wscript.shell
$obj.SendKeys([char]175)

** Each volume counter = 2%
Ex: Set Volume to 50%
Function Set-Speaker($Volume){ $ws = new-object -com wscript.shell;1..50 | % { $ws.SendKeys([char]174)};1..$Volume | % { $ws.SendKeys([char]175) } }
Set-Speaker(25)

PowerShell - Remote Add/Join Computers to domain

$domainAdminCred = Credential
$localAdminCred = Credential

$ScriptBlock = {
  param($domainAdminCred, $computer)
  $domain = "Domain"
  Write-Host "[processing '$computer' ...]"
  Add-Computer -DomainName $domain -Credential $domainAdminCred -OUPath "OU=group1,DC=testing,DC=com" -Restart -Force
}

$IP = "192.168.1.101"
$computer = "PC-01"
Invoke-Command $IP -ScriptBlock $ScriptBlock -ArgumentList $domainAdminCred, $computer -Credential $localAdminCred
$IP = "192.168.1.102"
$computer = "PC-02"
Invoke-Command $IP -ScriptBlock $ScriptBlock -ArgumentList $domainAdminCred, $computer -Credential $localAdminCred

Thursday, August 21, 2014

DHCP Testing Command

Bring up Command Prompt and run:
netsh dhcp show server

Will receive the following result:
-----------------------------------------------------------------------------------------------
1 Servers were found in the directory service:

        Server [pdcsc.server.com] Address [192.168.100.1] Ds location: cn=pdcs
.server.com

Command completed successfully.
-----------------------------------------------------------------------------------------------

Thursday, August 7, 2014

PowerShell - Remote Configure Static/DHCP IP Address

For Static IP:
$sc = {
param($IP)
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableStatic($IP, "255.255.255.0");
$wmi.SetGateways("192.168.1.1", 1);
$wmi.SetDNSServerSearchOrder(@("192.168.1.1","8.8.8.8"))
}

$IP = "192.168.1.101"
$job = Invoke-Command 192.168.1.121 -ScriptBlock $sc -ArgumentList $IP -AsJob -Credential $cred
Wait-Job $job -Timeout 20
$IP = "192.168.1.102"
$job = Invoke-Command 192.168.1.122 -ScriptBlock $sc -ArgumentList $IP -AsJob -Credential $cred
Wait-Job $job -Timeout 20



For DHCP IP:
$sc = {
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();
}

$job = Invoke-Command 192.168.1.101 -ScriptBlock $sc -AsJob -Credential $cred
Wait-Job $job -Timeout 20
$job = Invoke-Command 192.168.1.102 -ScriptBlock $sc -AsJob -Credential $cred
Wait-Job $job -Timeout 20


Wednesday, July 9, 2014

PowerShell - Uninstall Application for Lab Computers Simultaneously

# Loop through the server list
Get-Content -Path "C:\Scripts\Computers.txt" | %{

  # Define what each job does
  $ScriptBlock = {
    param($server)
    Write-Host "[processing '$server' inside the job]"
    $app = Get-WmiObject -Class Win32_Product -computer $server -Filter "Name = 'Java 7 Update 60'"
    $app.Uninstall()
    Start-Sleep 60
  }

  Write-Host "processing $_..."

  # Execute the jobs in parallel
  Start-Job $ScriptBlock -ArgumentList $_
}

Get-Job

While (Get-Job -State "Running")
{
  Start-Sleep 10
}

Get-Job | Receive-Job

Monday, June 30, 2014

PowerShell - Remove/Unjoin Computer from domain

$pc = "PCName"
$user = "Administrator"
$pass = cat C:\securestring.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$pc\$user",$pass
$domain = "Domain"
$user2 = "Administrator"
$pass2 = cat C:\securestring2.txt | convertto-securestring
$domcred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$domain\$user2",$pass2
Remove-Computer -Computer $pc -UnjoinDomainCredential $domcred -LocalCredential $cred -Workgroup workgroup -PassThru -Force -restart

PowerShell - Write Credentials to File


$FilePath = "C:"
Write-Host "Enter login as domain\id:"
Read-Host | Out-File $FilePath\id.txt

Write-Host "Enter user password:"
Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File $FilePath\securestring.txt

Wednesday, June 18, 2014

icacls - Manage file/folder permission to users via the command line

Manage in Command Prompt:
icacls C:\Users\Public\Desktop\Share /grant Everyone:(OI)(CI)F
Manage in PowerShell:
icacls C:\Users\Public\Desktop\Share /grant "Everyone:`(OI`)`(CI`)F"

WMIC - Uninstall application via the command Line

WMIC - Windows Management Instrumentation Command-line

Use Command Prompt and enter "WMIC" to bring up wmic mode
wmic:root\cli>

Generate a list of installed applications.
product get name

Uninstall a specific application (Ex: uninstall Java 7 Update 51)
product where name="Java 7 Update 51" call uninstall
Enter "Y" to continute the uninstallation process

Thursday, January 30, 2014

PowerShell - Change Computer Name

Execute the command below and restart the computer.

$computerName = Get-WmiObject Win32_ComputerSystem
$name = "New Computer Name"
$computername.rename("$name")

Tuesday, January 7, 2014

PowerShell - How to Enable-PSRemoting on Windows XP

1. Use Command Prompt and run "Secpol.msc" to bring up Local Security Settings.
2. Go to Local Policies -> Security Options
3. Double click on "Network access: Sharing and security model for local accounts"
4. Change to "Classic - local users authenticate as themselves" and hit OK.
5. Enter PowerShell and execute the following commands:
Enable-PSRemoting -Force
Set-Item wsman:\localhost\client\trustedhosts *
Restart-Service WinRM

Done!


Friday, November 15, 2013

Powershell - Enable Firewall Rules


netsh advfirewall firewall set rule group="Remote Administration" new Enable=yes
netsh advfirewall firewall set rule group="Remote Desktop" new Enable=yes
netsh advfirewall firewall set rule group="Remote Event Log Management" new Enable=yes
netsh advfirewall firewall set rule group="Remote Service Management" new Enable=yes
netsh advfirewall firewall set rule group="Remote Scheduled Tasks Management" new Enable=yes
netsh advfirewall firewall set rule group="Remote Volume Management" new Enable=yes
netsh advfirewall firewall set rule group="Windows Firewall Remote Management" new Enable=yes
netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new Enable=yes
netsh advfirewall firewall set rule group="File and Printer Sharing" new Enable=yes
netsh advfirewall firewall set rule group="Performance Logs and Alerts" new Enable=yes

Monday, July 15, 2013

PowerShell - Notes

Enabling PowerShell Remoting:
Enable-PSRemoting -Force

Workgroup Setup:
Set-Item wsman:\localhost\client\trustedhosts *

On both computers, restart the WinRM service so your new settings will take effect:
Restart-Service WinRM

Verify/Set the settings for the execution policy:
Get-ExecutionPolicy
Set-ExecutionPolicy Unrestricted

Starting a Remote Session:
Enter-PSSession -ComputerName "Computer name" -Credential "User name"