Powershell

Scan for Files/Folders Older Than 90 days

This script will scan through a list of UNC’s and it will locate any folders or files that are older than 90 days. You can change the amount of days by editing the $gtdays variable.

$servers = Get-Content "\\SERVER\"
$gtdays = "90"
$timeformat='yyyy/MM/dd hh:mm:ss tt'
$time = (Get-Date).ToString($timeformat) 
$drive = "v"
$drivemapped = $drive + ":"

$username = 'USERNAME'
$password = ConvertTo-SecureString -string 'PASSWORD' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -argumentlist $username, $password

foreach($server in $servers) {
    
    #cmd /c net use v: $server  | Out-Null
    New-PSDrive -name $drive -psprovider filesystem -root $server -Credential $cred  | Out-Null
    $date = Get-ChildItem $drivemapped| Sort-Object -Descending -Property LastWriteTime | Select-Object -First 1
    $days = (New-TimeSpan $date.LastWriteTime $time).Days
    #cmd /c net use v: "/delete" | Out-Null
    Remove-PSDrive $drive | Out-Null

    if($days -gt $gtdays) {
        New-Object –TypeName PSObject -Property @{
            'Server' = $server
            'File or Folder Name' = $date.Name
            'Date'= $date.LastWriteTime
            'Days Old'= $days
        }
        
    }
    else {
              
    }   
}

Bulk User and Computer Object Creation for DC Labs

Here are two scripts that will create a bulk amount of users and computer objects in Active Directory. It is set to create 50 objects but you can always adjust the amount in the $number variable.

Computer Creation:

$number= for($i=1; $i -le 50; $i++){$i}

foreach($num in $number){
    $computers = "Computer-" + $num
    New-ADComputer -Name $computers -Path "OU=Computers, OU=IT Department,DC=ITLab,DC=local" -Description "Test Computer"
}

User Creation:

$number= for($i=1; $i -le 50; $i++){$i}

foreach($num in $number){
    $user = "User" + $num
    $password = ConvertTo-SecureString "PASSword123$" -AsPlainText -Force;
    New-ADUser -Name $user -SamAccountName $user -DisplayName $user -AccountPassword $password -Enabled $true -Path "OU=Users, OU=IT Department,DC=ITLab,DC=local"
}

32bit Uninstall and Reinstall PowerShell Script

Below is a script that will automatically uninstall and reinstall a 32bit application.

Please note: This script only works with 32bit applications!

In order for this script to work, you will need to configure the following:
$Appname = Insert Application Name
$AppSetup = Insert Application File Path After MSIEXEC /I
$Silent = Insert Silent Install Switches
$SilentUninstall = Insert Silent Uninstall Switches

I am using TightVNC for the following example:

$appname = 'TightVNC'
$32bit = get-itemproperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' | Select-Object DisplayName, DisplayVersion, UninstallString, PSChildName | Where-Object { $_.DisplayName -match "^*$appname*"}
$appSetup="msiexec /i tightvnc-2.7.10-setup-64bit.msi"
$silent = " /quiet /norestart ADDLOCAL=Viewer"
$silentuninstall = " /qn"

if ($32bit -eq "" -or $32bit.count -eq 0) {
                Write-Host "Software is not Installed"
                Start-Sleep -s 3
                Clear-Host
                Write-Host "Starting Setup..."
                cmd /c ($appSetup + $silent)
                Clear-Host
                Write-Host "Installation Complete!"
                Start-Sleep -s 3
}
else {
    
                $Uninstallstring = $32bit.UninstallString -replace 'msiexec.exe /i','msiexec.exe /x'
                Write-Host "Uninstalling Software..."
                cmd /c ($uninstallstring + $silentuninstall)
                Clear-Host
                Write-Host "Starting Setup..."
                cmd /c ($appSetup + $silent)
                Clear-Host
                Write-Host "Installation Complete!"
                Start-Sleep -s 3
           
}

Enjoy!

Styling Your Powershell HTML Reports

When you are using Convertto-HTML, you can easily customize your reports by using the -Head, -Precontent, -Body, -Postcontent parameters.

You can insert HTML and CSS into these parameters by using a here-string.

Below is an example on how to use some of these parameters to customize an Event Log Report.

$Head = @"
<style>
body { background-color:#f6f6f6; font-family:calibri; margin:0px;}
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #34495e; color:#ffffff}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
TR:Nth-Child(Even) {Background-Color: #dddddd;}
</style>
"@
$precontentheader1 = @"
<div style="padding: 20px 0 5px;position: relative;top:0px;background-color: #34495e;margin-bottom:40px;">
<center>
<img src="https://www.joseespitia.com/wp-content/uploads/2015/11/JE-white.png">
<h2 style="color:#fff; font-size:55px;">
"@
$precontentheader2 = @"
</h2>
</div>
"@

$GetEventLog = Get-EventLog system -Newest 100 | Select-Object -property TimeGenerated, Source, Message, Entrytype |  Where-Object { $_.entrytype -eq "error" -or "warning" }
$Errorcount = $GetEventLog.Index.Count

$GetEventLog | ConvertTo-Html -Head $Head -PreContent "$precontentheader1 $Errorcount System Errors Found$precontentheader2" | Out-File Eventlogs.html

Convert Youtube Videos to MP3

Happy New Years!

The following script will use YoutubeinMP3.com’s API to download the audio from most YouTube Videos. All you have to edit is the $dest variable and insert the directory that you want your mp3’s to go.

Note: This does not work with all videos! I have contacted YoutubeinMP3.com to figure out why certain videos do not work.

try {
    $ytstring = Read-host "Please insert the Youtube Video ID"
    $json = Invoke-WebRequest "http://www.youtubeinmp3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=$ytstring"
    $dlurl = $json | ConvertFrom-Json | Select -Property link | Format-Table -HideTableHeaders | out-file url.txt
    $songtitle = $json | ConvertFrom-Json | Select -Property title | Format-Table -HideTableHeaders | out-file title.txt
    $filename = get-content title.txt
    $dest = "C:\Users\jespitia\Documents\Downloads\'$filename'.mp3"
    $url = get-content url.txt
    $uri = "$url"
    Invoke-Webrequest -Uri $uri -OutFile "$dest"
}
catch {
    write-host Please try another Video -ForegroundColor Red
}

Enjoy!

Search for Uninstall Strings

This Powershell Script will search through your registry and locate uninstall strings for your 32bit and 64bit programs. It is very easy to use and all you have to do is type in the name of the program to locate the uninstall string.
Read more

Automatic TeamViewer Download for Customers

Have you ever spent 20 minutes trying to explain how to download Teamviewer to a customer or family member? Well thankfully, we have a solution for this!
Read more

Get Movie Information from Powershell

I recently have been messing around with Powershell and during my scripting tests, I came across a cmdlet called Invoke-WebRequest. It is an extremely powerful cmdlet that can grab data from a web page or web service. As I started to read about it, I decided that it would be neat to grab XML data from a website and display it in Powershell. During my search for an api, I came across http://www.omdbapi.com/, which is a free service that contains movie information.
Read more

Page 6 of 6
« Newer Posts