psisContainer: Filter Directories using Powershell

To filter the folders (i.e., directories) available in the current context, the following property can be used with a predicate:$_.psiscontainer

In essence, it returns a boolean indicating whether the current object is a directory or not. Hence, the following script prints the absolute path of the each sub-directories from the current context.

ls –r | ?{$_.psiscontainer} | select fullname

PowerShell | Removing SVN bindings from a directory

There are several ways to remove svn binding from a directory and its sub-directories. However, in this post, we write a PowerShell script to perform this task.  To do so, we have to remove all the enclosing directories named “.svn“.   

Following PowerShell script first gets all the directories that we want remove and then, delete them one-by-one recursively.


ls -Include .svn -Recurse -Force | foreach { del $_.FullName -Recurse -Force }

PowerShell | Stopping processes using a PowerShell script

This post is about stopping a particular process or processes using PowerShell script. I often use this script to shutdown processes from shell.

A straightforward way to achieve this is as follows :

stop-process [pid]

However, above command requires the knowledge of the process id, which can be retrieved using get-process  PowerShell command. However, if the process id is not known at this point, the following command can also  do the job.

stop-process -processname [pname]

In addition , we could stop all the processes the matches certain criteria. For the sake of simplicity, let’s consider that we would like to stop the processes whose name matches with following “*.chrome” pattern. get-process returns all the processes currently active in the system. Then we  filter the processes with names that  matches the specified pattern  using the Where-Object as follows.

get-process  | ?{$_.ProcessName -like "[pattern]"}

For instance, if we use *.chrome as the pattern, currently it is giving following output in my shell :

image

Next, we perform projection on the returned objects to only select ProcessName and use a iterator to iterate over it and invoke stop-process as follows :

get-process | ?{$_.ProcessName -like "[pattern]"} |  %{$_.ProcessName}| foreach {stop-process -ProcessName $_ ;  echo $_}

Synopsis. Above code snippet stops all the processes whose name matches with the specified pattern and lists the name of the stopped processes. It went through several steps: filtering , projection and , finally iterating and stopping all the projected process names.

Hope it helps. Cheers!