Archive for February 2023

Windows – Credential Manger via command line

You can store and recall password with Windows Credential manger. Great to enter saved password to be used within batch scripts.

Base command is: cmdkey

Documentation on microsoft’s website

For Powershell use CredentialManger module

https://www.powershellgallery.com/packages/CredentialManager/2.0

Windows – Set various power settings via command line

Set various timeout setting for sleep, hibernate, disk, and monitor. Below will disable all timeouts. Great for VM workstations that need to run 24/7

powercfg /x -hibernate-timeout-ac 0
powercfg /x -hibernate-timeout-dc 0
 
powercfg /x -disk-timeout-ac 0
powercfg /x -disk-timeout-dc 0
 
powercfg /x -monitor-timeout-ac 0
powercfg /x -monitor-timeout-dc 0
 
Powercfg /x -standby-timeout-ac 0
powercfg /x -standby-timeout-dc 0

Powershell – Download a file

Invoke-WebRequest

Invoke-WebRequest -Uri https://example.com/files/myfile.exe -OutFile $env:USERPROFILE\Downloads\myfile.exe

Powershell – Install RAST (Remote Server Administration Tools)

These tools can be install on any workstation to administrate servers remotely. Command below will install all Window Server tools

Get-WindowsCapability -Online |? {$_.Name -like "*RSAT*" -and $_.State -eq "NotPresent"} | Add-WindowsCapability -Online

Windows – Compress each file in folder into 7z archive. Keep modified date and delete original.

for /f %i in ('forfiles /D -12/31/2011 /c "cmd /c echo @FILE"') do @7z a Archives/%~i.7z %~i -mmt=on -sdel -stl
  • Command is to be ran at same folder you want to compress every file into a single 7z.
  • Forfiles /D will only select files older than specified date. 
  • It will place each 7z into Archives subfolder.
  • Keeps the date of the file inside as archive date.
  • “-sdel” Delete file when done.

Windows Powershell – Manage Printers and Printer Ports

Reference this documenation for more commands and options. This works only in Windows 10
https://docs.microsoft.com/en-us/powershell/module/printmanagement/?view=win10-ps

here are some example commands to add port, view printers, and update their port configurations. These are running from batch shell.

powershell -Command "Add-PrinterPort -Name "10.3.0.50" -PrinterHostAddress "10.3.0.50" -SNMP 1 -SNMPCommunity public"
powershell -Command "Add-PrinterPort -Name "10.3.0.51" -PrinterHostAddress "10.3.0.51" -SNMP 1 -SNMPCommunity public"
powershell -Command "get-printer"
powershell -Command "Set-Printer -Name CanonBW -PortName "10.3.0.51""
powershell -Command "Set-Printer -Name CanonColor -PortName "10.3.0.50""

 Windows AD – Get list of computers in domain

Get-ADComputer -filter * | select name | ft

Windows – Get files older than 7 days then delete them

Get-ChildItem 'C:\path\to\files'  | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-7))} | Remove-Item

Windows – Test AD Credentials in Powershell

Function Test-ADAuthentication {
    param($username,$password)
    (new-object directoryservices.directoryentry "",$username,$password).psbase.name -ne $null
    }
 
 
Test-ADAuthentication "dom\myusername" "mypassword"

Exchange Online – Import deleted mailbox into a new mailbox

##Log into Exchange Online
Connect-ExchangeOnline
 
##Must be deleted inbox, view all deleted inboxes with `Get-Mailbox -SoftDeletedMailbox`
$from = "bogus@email.com"
 
##Must be normal exhcange inbox.
$to = "new-bogus@email.com"
 
##Getting Exhcange Object IDs
$fromID = Get-Mailbox -SoftDeletedMailbox -Identity $from  | select ExchangeObjectId
$toID = Get-Mailbox -Identity $to | select ExchangeObjectId
 
##Uncomment to run. be sure you have all the above information correct!
#New-MailboxRestoreRequest -SourceMailbox $fromID -TargetMailbox $toID -TargetRootFolder "Imported"
#New-MailboxRestoreRequest -SourceMailbox $fromID -TargetMailbox $toID 
 
##Verify/Monitor copy progress.
$ident = Get-MailboxRestoreRequest | select Identity
Get-MailboxRestoreRequestStatistics $ident.Identity
 
##good to disconnect
Disconnect-ExchangeOnline