PowerShell: How do I convert an array object to a string in PowerShell?

$a=”This”, ‘Is’, ‘a’, ‘cat’ Using double quotes (and optionally use the separator $ofs) # This Is a cat “$a” # This-Is-a-cat $ofs=”-” # after this all casts work this way until $ofs changes! “$a” Using operator join # This-Is-a-cat $a -join ‘-‘ # ThisIsacat -join $a Using conversion to [string] # This Is a cat … Read more

How do I get only directories using Get-ChildItem?

For PowerShell 3.0 and greater: Get-ChildItem -Directory You can also use the aliases dir, ls, and gci For PowerShell versions less than 3.0: The FileInfo object returned by Get-ChildItem has a “base” property, PSIsContainer. You want to select only those items. Get-ChildItem -Recurse | ?{ $_.PSIsContainer } If you want the raw string names of … Read more

What’s the best way to determine the location of the current PowerShell script?

PowerShell 3+ # This is an automatic variable set to the current file’s/module’s directory $PSScriptRoot PowerShell 2 Prior to PowerShell 3, there was not a better way than querying the MyInvocation.MyCommand.Definition property for general scripts. I had the following line at the top of essentially every PowerShell script I had: $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition