-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathCurrent.directory.with.odd.name.ps1
48 lines (37 loc) · 1.22 KB
/
Current.directory.with.odd.name.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# results to be shown and tested
$results = New-Object System.Collections.Specialized.OrderedDictionary
# this script directory
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Definition
# to catch Get-Item errors
$ErrorActionPreference = 'Stop'
# makes a directory and a directory 'test' in it
function New-TestDirectory($Path) {
$test = "$Path/test"
if (![System.IO.Directory]::Exists($test)) {$null = mkdir $test}
}
### test 1, cd `[1] and test `[1]\test
$odd = Join-Path $PSScriptRoot '`[1]'
New-TestDirectory $odd
Set-Location -LiteralPath $odd
# the current path
$results.Location1 = (Get-Location).Path
# the item 'test' is there
$results.GetChildItem1 = Get-ChildItem
# fails
$results.GetItemNormal1 = try {Get-Item test} catch {$_}
# $fails
$results.GetItemLiteral1 = try {Get-Item -LiteralPath test} catch {$_}
### test 2, cd [1] and test [1]\test
$odd = Join-Path $PSScriptRoot '[1]'
New-TestDirectory $odd
Set-Location -LiteralPath $odd
# the current path
$results.Location2 = (Get-Location).Path
# the item 'test' is there
$results.GetChildItem2 = Get-ChildItem
# works now
$results.GetItemNormal2 = Get-Item test
# still fails
$results.GetItemLiteral2 = try {Get-Item -LiteralPath test} catch {$_}
# output
$results