Archive | July, 2012

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 →

PowerShell: Finding Files Owned by a User

Here’s a simple filter that will show only those files and folders that a specific user is owner of: filter Get-Owner ($Account=”$env:UserDomain\$env:USerName”) { if ((Get-ACL $_.Fullname).Owner -like $Account) { $_ } } It is very easy to use. By default, it checks for files and folders owned by you: PS> Dir $home | Get-Owner You […]

Continue Reading →

PowerShell: Stopping Services Remotely

Stop-Service cannot stop services remotely. One easy way of doing so is Set-Service: Set-Service -Name Spooler -Status “Stopped” -ComputerName targetcomputer However, unlike Stop-Service, Set-Service has no -Force parameter, so you cannot stop services if they, for example, have running dependent services. If your infrastructure supports PowerShell Remoting, you could use Invoke-Command instead: Invoke-Command { Stop-Service […]

Continue Reading →
revia in alcoholism