Category Archives: PowerShell

Command to Verify the PowerShell Version

The following command gets you the PowerShell version.

(Get-Host).Version

Get-PowerShell-Version

File Name Search in PowerShell

Applies to: Microsoft PowerShell

Here a simple but effective command to retrieve a list of all files in a directory tree containing a specific text in the file name.

dir c:users -r -i “*yourtext*”

Explanation

  • The first argument is the directory to search (c:users).
  • Process all sub-directories with the –r switch.
  • The -i switch is used to specify which file names to search for, in this case all files containing yourtext

File Content Search in PowerShell

There is a way to use Powershell to search the contents of a file.  This method will not use the Windows index so it will be slow, but it does work.

get-childitem c:users -filter *.txt -recurse | select-string -list -pattern “yourtext” | foreach {$_.Path}

Explanation

  • get-childitem: Get the children for a container.  In this example the container is a folder on the file system.
    • The first argument is the directory to search (c:users).
    • The –filter switch is used to limit what files to search, in this example it will search files with a txt extension.
    • Process all sub-directories with the –recurse switch.
  • select-string: Find text in strings or files.
    • The –list switch stops the search within the source once the match is found.  In other words it will not waste time looking for other occurrences of the text.
    • The –pattern switch is where you specify the search text.  Regex is permitted.
  • foreach:  Iterate the resulting files.
  • $_.Path:  Output the full path for the file.

Test-Run a PowerShell or Exchange Management Shell Command

Applies to: PowerShell, Exchange Server

Exchange Management Shell/PowerShell commands allow the addition of the “-whatif” parameter, which will test your command without actually executing it.

Example: Set-Mailbox -Identity testmailbox -DisplayName “Test User” -whatif