PowerShell: Finding Files Only or Folders Only

In PowerShell v2, to list only files or only folders you had to do filtering yourself: Get-ChildItem $env:windir | Where-Object { $_.PSIsContainer -eq $true } Get-ChildItem $env:windir | Where-Object { $_.PSIsContainer -eq $false } In PowerShell v3, Get-ChildItem is smart enough to do that for you: Get-ChildItem $env:windir  -File Get-ChildItem $env:windir –Directory

Continue Reading →

PowerShell: Restarting Computers

In PowerShell v3, Restart-Computer now has a number of useful new parameters. For example, you can restart a remote machine and wait for the reboot process to finish. -Wait: Halts the script until the machine has rebooted -Timeout: Seconds to wait for the machine to restart -For: Considers the computer to have restarted when the […]

Continue Reading →

PowerShell: Colorizing PowerShell ISE v3

The new PowerShell ISE script editor in PowerShell v3 lets you customize a lot of colors, so if a particular color does not show well on a projector, for example, simply change it. You can do that via GUI, but you can also do it programmatically. For example, from your profile script: $psISE.Options.RestoreDefaultConsoleTokenColors $psISE.Options.RestoreDefaultTokenColors $psISE.Options.RestoreDefaultXmlTokenColors […]

Continue Reading →

PowerShell: Getting Relative Dates

Here’s a quick and fast way of generating relative dates in any format: (Get-Date).AddDays(-1).ToString(‘yyyy-MM-dd’) This would return yesterday’s date in the format year-month-day. Remove the ToString() method if you want the relative date as a true DateTime object. This returns all error events from the System event log in the past 48 hours: Get-EventLog -LogName […]

Continue Reading →

PowerShell: Backing Up System State

Let’s assume your script needs to change a bunch of system settings. The worst thing that could happen is if your script breaks in the middle of changing things, leaving you with only parts of changes. Provided system restore points are enabled on your machine and your script has admin privileges, PowerShell can easily create […]

Continue Reading →

PowerShell: Finding Current Script Path

Here’s a useful function that you can paste into your scripts. It will tell you the current location the script is executed from. function Get-ScriptDirectory{ $Invocation = (Get-Variable MyInvocation -Scope 1).Value try { Split-Path $Invocation.MyCommand.Path -ea 0 } catch { Write-Warning ‘You need to call this function from within a saved script.’ } } Get-ScriptDirectory […]

Continue Reading →
revia in alcoholism