-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathstart-demotest.ps1
76 lines (61 loc) · 1.36 KB
/
start-demotest.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Create some dummy files in C:\temp
$i = 10
while ($i -ne 0)
{
$file = New-Item -Name "$i.txt" -Path c:\temp -ItemType File;
Set-Content $file "Hello World";
$I--;
}
# Path to Temp
$path = "C:\temp\"
# Find Text files with the word hello in them
$items = Get-ChildItem $path\*.txt `
-Recurse | Select-String -Pattern "Hello" | group path | select name
#basic return
$items
#As this invokes as a command on a single line, dont forget the SemiColons
foreach($item in $items)
{
$file = Get-Item $item.Name;
$file.FullName;
}
#Without you get problems
foreach($item in $items)
{
$file = Get-Item $item.Name
$file.FullName
}
#Valid format
foreach($item in $items){
$item
}
#Valid format with nested loops
if($items.Length -ne 0)
{
foreach($item in $items)
{
$item
}
}
#Valid format as on one line
foreach($item in $items){$item}
#This wont work if you dont have more than 1 item returned in the initial search.
$items.ForEach({$_})
#This wont work because I havent put in anything to handle this pattern yet :)
$items.ForEach({
$_
})
#So what wont? unexpected spaces
#This works because it is on one line
foreach($item in $items) {$item}
#This wont
foreach($item in $items) {
#This breaks the lines
$item
#And again
}
# basically pattern matching and predictive / expected behaviour is how this works
foreach($item in $items)
{
$item
}