Create a directory if not exists
#create directory
if (!(Test-Path "C:\Directory")) {
New-Item -ItemType Directory -Path "C:\Directory"
}
Remove directory if exist
#remove directory including all (sub)folders and files in it
if (Test-Path "C:\Directory") {
Remove-Item -path "C:\Directory" -Recurse -Force
}
Delete item if exists
#remove item
if (Test-Path "C:\Users\Public\Desktop\Shortcut.lnk") {
Remove-Item -path "C:\Users\Public\Desktop\Shortcut.lnk" -Force
}
Rename Folder
#Rename Folder
try {
Rename-Item -Path "C:\FolderName" -NewName "C:\NewFolderName"
} catch {
}
Rename File
#Rename file
try {
Rename-Item -Path "C:\temp\FileName.txt" -NewName "C:\temp\NewFileName.txt"
} catch {
}
Copy a file
#Copy file
if (Test-Path "C:\NewLocation") {
Copy-Item -Path "C:\CurrentLocation\FileName.txt" -Destination "C:\NewLocation\FileName.txt" -Force
}
Copy all files and folders
#Copy all files and folders
if (Test-Path "C:\NewLocation") {
Copy-Item -Path "C:\CurrentLocation\*" -Destination "C:\NewLocation" -Recurse -Force
}
Copy a file to all user profiles – PSADT
Copy-FileToUserProfiles -Path "$dirfiles\Shortcut.lnk" -Destination "Desktop" -Recurse
Remove a file in all user profiles – PSADT
Remove-FileFromUserProfiles -Path "Desktop\Shortcut.lnk" -Recurse
Stop running Service
#stop the service if it's running
if (Get-Service -Name "ServiceName" -ErrorAction SilentlyContinue | Where-Object { $_.Status -eq "Running" }) {
Stop-Service -Name "ServiceName" -Force -ErrorAction SilentlyContinue
}
Stop running Process
#stop the process if it's running
$proc = Get-Process -Name "ProcessName" -ErrorAction SilentlyContinue
if ($proc) {
Stop-Process -Name "ProcessName" -Force -ErrorAction SilentlyContinue
}
Unblock files all files in a folder
#unblock files
$path = "C:\Files"
Get-ChildItem -Path $path -Recurse | Unblock-File
Create local user and set password and add it to the local admin group
#generate a random password
$PW = Get-Random
$SecurePW = ConvertTo-SecureString -String $PW -AsPlainText -Force
#define the username
$UserName = "NewLocalUser" # Replace with desired username
#create the local user
try {
New-LocalUser -Name $UserName -Password $SecurePW -FullName "Local Admin User" -Description "Created via script" -PasswordNeverExpires $true
Write-Host "User '$UserName' created successfully."
} catch {
Write-Warning "Failed to create user '$UserName': $_"
}
#add user to the local Administrators group using SID
try {
$adminGroupSID = 'S-1-5-32-544'
Add-LocalGroupMember -Group $adminGroupSID -Member $UserName
Write-Host "User '$UserName' added to Administrators group."
} catch {
Write-Warning "Failed to add user '$UserName' to Administrators group: $_"
}
Set folder permissions to full control for all users
try {
#grant Full Control (F) to the Users group (SID: S-1-5-32-545) on C:\Folder, recursively
icacls.exe "C:\Folder" /grant "*S-1-5-32-545:(OI)(CI)F" /T
Write-Host "Permissions successfully applied."
} catch {
Write-Warning "Failed to set permissions: $_"
}
Add ODBC entries – PSADT
#add 32-bit ODBC DSNs
$odbcEntries = @(
@{ Name = "ABC"; Server = "oracle"; User = "ABC" },
@{ Name = "DEF"; Server = "oracle"; User = "DEF" },
@{ Name = "GHI"; Server = "oracle"; User = "GHI" }
)
foreach ($entry in $odbcEntries) {
$dsnPath = "HKLM:\SOFTWARE\WOW6432Node\ODBC\ODBC.INI\$($entry.Name)"
$sourcesPath = "HKLM:\SOFTWARE\WOW6432Node\ODBC\ODBC.INI\ODBC Data Sources"
try {
Write-Log -Message "Creating ODBC DSN registry path: $dsnPath" -Severity 1
New-Item -Path $dsnPath -Force -ErrorAction Stop | Out-Null
Write-Log -Message "Setting ODBC DSN values for '$($entry.Name)'" -Severity 1
Set-ItemProperty -Path $dsnPath -Name "Driver" -Value "C:\Windows\SysWOW64\msorcl32.dll" -ErrorAction Stop
Set-ItemProperty -Path $dsnPath -Name "Server" -Value $entry.Server -ErrorAction Stop
Set-ItemProperty -Path $dsnPath -Name "UID" -Value $entry.User -ErrorAction Stop
Write-Log -Message "Adding DSN '$($entry.Name)' to ODBC Data Sources list" -Severity 1
if (-not (Test-Path -Path $sourcesPath)) {
Write-Log -Message "ODBC Data Sources key missing, creating it." -Severity 2
New-Item -Path $sourcesPath -Force | Out-Null
}
Set-ItemProperty -Path $sourcesPath -Name $entry.Name -Value "Microsoft ODBC for Oracle" -ErrorAction Stop
Write-Log -Message "Successfully added '$($entry.Name)' to ODBC Data Sources." -Severity 1
Write-Log -Message "Successfully created ODBC DSN: $($entry.Name)" -Severity 1
}
catch {
Write-Log -Message "Failed to create ODBC DSN '$($entry.Name)': $_" -Severity 3
}
}
Remove ODBC entries – PSADT
#remove 32-bit ODBC DSNs
$odbcEntries = @(
@{ Name = "ABC" },
@{ Name = "DEF" },
@{ Name = "GHI" }
)
foreach ($entry in $odbcEntries) {
$dsnPath = "HKLM:\SOFTWARE\WOW6432Node\ODBC\ODBC.INI\$($entry.Name)"
$sourcesPath = "HKLM:\SOFTWARE\WOW6432Node\ODBC\ODBC.INI\ODBC Data Sources"
try {
Write-Log -Message "Removing ODBC DSN registry path: $dsnPath" -Severity 1
if (Test-Path -Path $dsnPath) {
Remove-Item -Path $dsnPath -Recurse -Force -ErrorAction Stop
Write-Log -Message "Successfully removed DSN path for '$($entry.Name)'" -Severity 1
} else {
Write-Log -Message "DSN path '$($entry.Name)' not found, skipping." -Severity 2
}
Write-Log -Message "Removing entry from ODBC Data Sources for '$($entry.Name)'" -Severity 1
if (Test-Path -Path $sourcesPath) {
if (Get-ItemProperty -Path $sourcesPath -Name $entry.Name -ErrorAction SilentlyContinue) {
Remove-ItemProperty -Path $sourcesPath -Name $entry.Name -ErrorAction Stop
Write-Log -Message "Successfully removed '$($entry.Name)' from ODBC Data Sources." -Severity 1
} else {
Write-Log -Message "ODBC Data Sources entry '$($entry.Name)' not found." -Severity 2
}
} else {
Write-Log -Message "ODBC Data Sources key does not exist, skipping." -Severity 2
}
}
catch {
Write-Log -Message "Failed to remove DSN '$($entry.Name)': $_" -Severity 3
}
}
Delete all files in folder and subfolders except of dll’s
$folderPath = "path"
#get all files in the folder and subfolders except .dll files
Get-ChildItem -Path $folderPath -Recurse -File | Where-Object { $_.Extension -ne ".dll" } | ForEach-Object {
try {
Remove-Item $_.FullName -Force
Write-Host "Deleted:" $_.FullName
} catch {
Write-Warning "Could not delete:" $_.FullName
}
}
Delete all files in folder and subfolders except all unsigned dll’s
#set the path to your main folder
$folderPath = "path"
#get all files in the folder and subfolders
Get-ChildItem -Path $folderPath -Recurse -File | ForEach-Object {
try {
if ($_.Extension -eq ".dll") {
#check digital signature
$signature = Get-AuthenticodeSignature $_.FullName
if ($signature.Status -eq "Valid") {
#delete signed DLLs
Remove-Item $_.FullName -Force
Write-Host "Deleted signed DLL:" $_.FullName
} else {
Write-Host "Kept unsigned DLL:" $_.FullName
}
} else {
#delete all non-DLL files
Remove-Item $_.FullName -Force
Write-Host "Deleted non-DLL:" $_.FullName
}
} catch {
Write-Warning "Could not delete:" $_.FullName
}
}