Skip to content

Instantly share code, notes, and snippets.

View mavaddat's full-sized avatar
🏠
Working from home

Mavaddat Javid mavaddat

🏠
Working from home
View GitHub Profile
@mavaddat
mavaddat / txtToWord.ps1
Last active May 2, 2023 22:17
Txt to Word Doc using PowerShell
#Requires -Modules Pscx
# First load MS Office base assemblies into the environment
Add-Type -Path $env:WINDIR\assembly\GAC_MSIL\office\*\office.dll -PassThru # Use -PassThru to verify it worked (otherwise, fails silently)
# Then, add the target MS Word assembly
Get-ChildItem -Path $env:windir\assembly -Recurse -Filter Microsoft.Office.Interop.Word* -File | ForEach-Object {
Add-Type -LiteralPath ($_.FullName) -PassThru # PassThru again to verify
}
$wordApp = New-Object -ComObject Word.Application
if($null -eq $wordApp){
Write-Host "Failed to create Word.Application object"
@mavaddat
mavaddat / resolveOneDriveConflicts.ps1
Last active June 15, 2023 16:49
Resolve OneDrive conflicts in automatically renamed files
$winMergeExe = "$env:LOCALAPPDATA\Programs\WinMerge\WinMergeU.exe"
$oneDrivePaths = Get-Item -Path Env:\OneDrive* | Select-Object -Unique -ExpandProperty Value
# $duplicatePattern = New-Object -TypeName regex -ArgumentList @('(.+)(?:-[^-\s]+$|\.[^\.]+\.bak$)', [System.Text.RegularExpressions.RegexOptions]::Compiled,[timespan]::FromMilliseconds(100))
$duplicatePattern = New-Object -TypeName regex -ArgumentList @("(.+)(?:-${env:COMPUTERNAME}(?:-\d+)*`$)", [System.Text.RegularExpressions.RegexOptions]::Compiled,[timespan]::FromMilliseconds(100))
foreach($onedrive in $oneDrivePaths){
Get-ChildItem -Path $onedrive -File -Recurse | Where-Object -FilterScript { $_.BaseName -match $duplicatePattern -and (Test-Path -Path (Join-Path -Path ($_.Directory.FullName) -ChildPath ($Matches[1] + $_.Extension)) -PathType Leaf)} | ForEach-Object {
$original = Join-Path -Path ($_.Directory.FullName) -ChildPath ($Matches[1] + $_.Extension)
$duplicate = $_.FullName
$originalHash = Get-FileHash -
@mavaddat
mavaddat / installFont.ps1
Last active May 2, 2023 01:52
Install and uninstall fonts using PowerShell
#Requires -Assembly "System.Windows.Media.GlyphTypeface, PresentationCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
#Requires -PSEdition Core
function Install-Font {
param
(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
$FontPath
)
@mavaddat
mavaddat / enableSnapins.ps1
Last active June 20, 2023 23:42
Suite of PowerShell commands to enable all MMC Snapins on Windows (requires admin UAC)
#Requires -RunAsAdministrator
Get-ChildItem -Path 'HKCU:\SOFTWARE\Policies\Microsoft\MMC\' | ForEach-Object { Set-ItemProperty -Path $_.PSPath -Name Restrict_Run -Value 1 # Disable is 1 (1 means allow the MMC)
}
Set-ItemProperty -Path HKCU:\Software\Policies\Microsoft\MMC\ -Name RestrictAuthorMode -Value 0
Set-ItemProperty -Path HKCU:\Software\Policies\Microsoft\MMC\ -Name RestrictToPermittedSnapins -Value 0
if(Test-Path -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\DisallowRun){
Remove-Item -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\DisallowRun
}
Set-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\System -Name DisableRegistryTools -Value 0 -Force
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\System\ -Name AllowDomainPINLogon -Value 1 -Type DWord
@mavaddat
mavaddat / getAcronym.ps1
Last active April 18, 2023 02:30
Calculate the acronym for a string
# Converts any text into acronym
function Get-Acronym
{
<#
.SYNOPSIS
Converts a string into its acronym form.
.DESCRIPTION
This function "acronymize" a string given in entry
@mavaddat
mavaddat / Start-WinGetUpdate.ps1
Last active April 13, 2023 16:33
Install winget using script developed by @HarmVeenstra.
function Start-WinGetUpdate {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false, HelpMessage = "Decide if you want to skip the WinGet version check, default it set to false")]
[switch]$SkipVersionCheck = $false
)
#Check if script was started as Administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
Write-Error ("{0} needs admin privileges, exiting now...." -f $MyInvocation.MyCommand)
@mavaddat
mavaddat / ExampleTask.ps1
Last active April 8, 2023 22:11
Write Progress Template for PowerShell
$Script:Timeout = 30
# Suppose we need to do work on a list of files, and we want to show the user a progress bar
$files = Get-ChildItem -File
[scriptblock]$task = {
[CmdletBinding()]
param (
[Parameter()]
[System.IO.FileSystemInfo]
$File
)
@mavaddat
mavaddat / CloseElements.ps1
Last active March 29, 2023 13:00
Close Elements SGML to XML
function Close-Elements
{
[CmdletBinding()]
param(
[Parameter(Mandatory, ParameterSetName="ReaderGiven", ValueFromPipeline, Position = 0)]
[ValidateNotNullOrEmpty()]
[System.Xml.XmlReader] $Reader,
[Parameter(Mandatory, ParameterSetName="InPathGiven", ValueFromPipeline, Position = 0)]
[ValidateNotNullOrEmpty()]
[string] $InPath,
@mavaddat
mavaddat / ImportPackage.ps1
Last active January 8, 2025 21:13
Import-Package PowerShell cmdlet
<#PSScriptInfo
.VERSION 1.0
.GUID ed896e0c-b20d-4d70-b894-ac698a1032cb
.AUTHOR Mavaddat Javid
.COMPANYNAME Mav Jav Education
@mavaddat
mavaddat / dtd2xsd.pl
Last active March 20, 2023 05:05
A Conversion Tool from DTD to XML Schema
#! perl
#
# Mavaddat Javid (mavaddat.ca) 2023/03/21
# derived from
#
# Mary Holstege ([email protected]) 2001/05/15
# derived from
#
# Yuichi Koike ($Id: dtd2xsd.pl,v 1.2 2001/05/15 13:08:21 mholstege Exp $)
# derived from