Programmatic Date/Time with Powershell

If you are scripting and logging your actions you will want to incorporate a timestamp of sorts. The following is a solid way of doing so:

  • In Powershell: get-date -format ^"{yyyy-MM-dd HH:mm:ss}^"
  • In CMD you would just call Powershell: powershell get-date -format ^"{yyyy-MM-dd HH:mm:ss}^"
  • Send your output to a log file: powershell get-date -format ^"{yyyy-MM-dd HH:mm:ss}^" >> C:\logs\scriptlog.txt

String Search in Directory of TXT Files using Powershell

If you work with ASCII text files a lot, you may find the need to search for a string within any of those text files at the directory level instead of each individual file. Here’s how to do that:

  • Select-String -Path c:\fso\*.txt, c:\fso\*.log -pattern modified,lastmod

This will search anything .txt or .log within c:\fso for the string “modified” or “lastmod” and include all results together in a list sent to the console.

Removing Stubborn Printer Drivers

Occasionally you’ll find the need to manually remove a printer driver, and it may put up a fight saying it’s still in use and can’t be removed. One way or another you can remove this but it requires a reboot, whether you boot into Safe Mode and remove it, or do it my preferred way:

  • Open regedit
  • In 32-bit browse to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\Printer_Model\InfPath, or 64-bit browse to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3\Printer_Model\InfPath
  • Delete the key representing the printer in question. This will disable the computer from loading its driver at startup.
  • Reboot, and you may want to manually delete the driver files. Their locations can be found in Print Management (printmanagement.msc)

Push GPUpdate to a List of Computers

Is GPupdate not working correctly? Wouldn’t it be nice to not have to go around to each workstation and run GPupdate manually in lieu of this? You can in fact push it out to a list of specified computers in a text file via Powershell. In this example I am also using PSexec to run the command.

foreach($adcomputer in Get-Content adcomputers.txt) {
	$command = {
		psexec -s \\$adcomputer gpupdate /force /wait:0
	}
	Start-Job -ScriptBlock $command
}

If you run this for one workstation, it will create a new job on your computer to run the command on that workstation, meaning a new instance of Powershell and everything that goes along with it. If you’re trying to push this out to 400 computers, it basically tells your computer to open 400 Powershell windows under the hood, and it will likely max out a standard 8GB of RAM, so I would suggest pushing it out in segments.

Connect to O365 Exchange Shell

On Windows servers running Exchange, the Exchange Management Shell is nice to have for configuration tasks. What if you’ve migrated to Office 365 and don’t have on-premises Exchange anymore? Just do the following, with administrative rights.

  • Set-ExecutionPolicy RemoteSigned
  • $UserCredential = Get-Credential
  • $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
  • Import-PSSession $Session -DisableNameChecking

And when you’re finished, be sure and disconnect gracefully from O365: Remove-PSSession $Session