How AI, OpenAI, and ChatGPT impact business and software.
One man loves powershell once he failed
1. 1
One man loves PowerShell
once he failed
@gab_km
12th July, 2014
Japan PowerShell meetup #3
2. 2
Who am I?
● an engineer works in an IT services company
● use .NET mainly, love F# in private.
● be interested in Python, D language, ...
● and PowerShell!
3. 3
The topics I will tell you about
● I was bad at PowerShell, but now feel to fit it
● I found the way how to overcome to struggle
5. 5
Once upon a time...
In this tweet, I have started PowerShell 2
years ago from this time(24/Mar/2011).
6. 6
What made me hard for PowerShell
There were 2 cases I had trouble using
PowerShell:
● I seemed there are a lot of differences in
command system between PowerShell and bat.
● I could not some operations shortly in
PowerShell while I could them in good old bat.
8. 8
In these days...
I tweeted this in the spring of 2013.
It makes me improve doing in PowerShell that I
tried to write some PS1 things for a year.
9. 9
Case 1
First I wrote bat files which were executed in
booting my PC to run some useful tools like a
mailer, connect file servers or do something
convenient.
10. 10
Case 1
First I wrote bat files which were executed in
booting my PC to run some useful tools like a
mailer, connect file servers or do something
convenient.
↓
When I was learning Python, I rewrote these
booting scripts in Python, which helped me to
learn it.
11. 11
Case 1
First I wrote bat files which were executed in
booting my PC to run some useful tools like a
mailer, connect file servers or do something
convenient.
↓
When I was learning Python, I rewrote these
booting scripts in Python, which helped me to
learn it.
↓
I tried rewriting them in PowerShell again for
my learning PowerShell.
13. 13
Case 2
Chocolatey is one of the best package managers
for Windows and I like it. As I should connect
using a proxy server in my workplace, this
package manager fails installing. :-(
So I sent a pull request to achieve 'chocolatey
install' with a proxy server.
14. 14
Case 2
Chocolatey is one of the best package managers
for Windows and I like it. As I should connect
using a proxy server in my workplace, this
package manager fails installing. :-(
So I sent a pull request to achieve 'chocolatey
install' with a proxy server.
* As of 12th July, 2014, this pull request is not merged.
16. 16
Case 3
# Rename all mp4 files with “Last Write Time”
Get-ChildItem |
Where { $_.Name.StartsWith("MAKERNAME") } |
ForEach { Rename-Item $_ -newname ($(Get-ItemProperty
$_).LastWriteTime.ToString("yyyyMMdd_hhmm") + ".MP4") }
17. 17
What gave me the power to the shell
I overcame the troubles in using PowerShell
with seeing the way to do things above.
● File specification and execution
● File operation
● Pipeline
18. 18
What gave me the power to the shell
I overcame the troubles in using PowerShell
with seeing the way to do things above.
● File specification and execution
● File operation
● Pipeline
Even if you are a person who are not good at
PowerShell, the abilities to do the 3 things
above make you break down the obstacle.
20. 20
File specification and execution
PS> notitle.ps1
This is a script file named 'notitle.ps1' and
created in your current directory.
You input like above to execute it.
If it does well, your terminal shows you
“Hello, there!” as output message.
26. 26
To specify files(2)- Use '.'
PS> .notitle.ps1
Hello, there!
PS>
When you want to specify a file or a folder in
your current directory, give '.' to the head
of a relative path.
27. 27
Case - You cannot execute a file
PS> $myScript = “C:myposhnotitle.ps1”
PS> $myScript
There are some cases where you cannot execute a
file with the way we have seen.
28. 28
PS> $myScript = “C:myposhnotitle.ps1”
PS> $myScript
C:myposhnotitle.ps1
PS>
Case - You cannot execute a file
29. 29
PS> $myScript = “C:myposhnotitle.ps1”
PS> $myScript
C:myposhnotitle.ps1
PS>
Though we want to execute a file, this way
shows us only the path we set.
Case - You cannot execute a file
30. 30
To execute files(1)- Use '&'
PS> & $myScript
Hello, there!
PS>
In this case, you can pass a path to execute to
an ampersand symbol('&') and you will execute
it.
The ampersand symbol works whether there are
spaces between the symbol and the path or not.
31. 31
To execute files(2)- Use '.'
PS> . $myScript
Hello, there!
PS>
In the same case, when you use a dot symbol(.)
with a path, you will execute it.
This method is called 'Dot-Sourcing'.
32. 32
To execute files(2)- Use '.'
PS> . $myScript
Hello, there!
PS>
In the same case, when you use a dot symbol(.)
with a path, you will execute it.
This method is called 'Dot-Sourcing'.
For more details, you will find when you search
the words “dot sourcing” or ask PowerShell
wizards.( ◜◡◝ )
35. 35
File operation
● Copy
● Rename
● Delete
There were demands for files to:
Though we can use 'good old' commands from cmd,
we are born to be PowerShell geeks and we are
wannabes to use Cmdlet.
36. 36
Copy files(1)
PS> Copy-Item -Path .notitle.ps1 -Destination C:myetc
PS> ls C:myetc | where Name -eq notitle.ps1
ディレクトリ : C:myetc
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014/07/11 23:42 31 notitle.ps1
PS>
When we want to copy files, we use Copy-Item.
37. 37
Copy files(2)
PS> Get-ChildItem C:myetc | % { $_.Name }
PS> Copy-Item -Path C:myposhn* -Destination C:myetc
PS> Get-ChildItem C:myetc | % { $_.Name }
As -Path parameter of Copy-Item understands
wildcard notations, we can use this cmdlet as
following:
38. 38
PS> Get-ChildItem C:myetc | % { $_.Name }
PS> Copy-Item -Path C:myposhn* -Destination C:myetc
PS> Get-ChildItem C:myetc | % { $_.Name }
notitle.ps1
nanimoyarukishinai.txt
PS>
Copy files(2)
As -Path parameter of Copy-Item understands
wildcard notations, we can use this cmdlet as
following:
39. 39
Rename files
PS> ls | % { $_.Name }
notitle.ps1
PS> Rename-Item -Path .notitle.ps1 -NewName foobar.ps1
PS> ls | % { $_.Name }
foobar.ps1
PS>
Use Rename-Item to rename files.
40. 40
Delete files
PS> ls | % { $_.Name }
notitle.ps1
PS> Remove-Item .notitle.ps1
PS> ls | % { $_.Name }
PS>
Use Remove-Item to delete files.
41. 41
Delete files
PS> ls | % { $_.Name }
notitle.ps1
PS> Remove-Item .notitle.ps1
PS> ls | % { $_.Name }
PS>
Use Remove-Item to delete files.
In PowerShell, cmdlets are defined by setting
their names on “Verb-Objective” rules, so we
can find them intuitively.
43. 43
Pipeline
There is a strong relationship like this:
“Associate PowerShell with pipeline,
44. 44
Pipeline
There is a strong relationship like this:
“Associate PowerShell with pipeline,
associate pipeline with F# PowerShell”
45. 45
Pipeline
in my humble opinion.
● filtering
● serial processing
There is a strong relationship like this:
“Associate PowerShell with pipeline,
associate pipeline with F# PowerShell”
When we want to use pipeline, the reason is
classified into 2 broad categories:
46. 46
Pipeline - filtering
@("hoge", "fuga", "bar") | where { $_.Length -eq 4 }
# "hoge"
# "fuga"
Filtering is one of the basis operations for
pipeline.
Returns only the elements of the collection for
which the given condition block returns $True.
47. 47
Pipeline - filtering
@("hoge", "fuga", "bar") | where { $_.Length -eq 4 }
# "hoge"
# "fuga"
Filtering is one of the basis operations for
pipeline.
Returns only the elements of the collection for
which the given condition block returns $True.
$_ is the variable for the current value in the
pipeline.
When I was one of the PowerShell newbies, I had
trouble remembering the variable and searched
again and again...
48. 48
Pipeline - serial processing(1)
@("hoge", "fuga", "bar") | foreach { $_.ToUpper() }
# "HOGE"
# "FUGA"
# “BAR”
Applies the given block for ForEach-Object to
each element of the collection.
49. 49
Pipeline - serial processing(1)
@("hoge", "fuga", "bar") | foreach { $_.ToUpper() }
# "HOGE"
# "FUGA"
# “BAR”
Applies the given block for ForEach-Object to
each element of the collection.
It is similar to the functions map, iter or
something like these in your natural functional
programming languages.
50. 50
Pipeline - serial processing(2)
@(1 .. 3) | % { $_ + 2 }
# 3
# 4
# 5
There is an useful alias for Foreach-Object:
'%' symbol.
51. 51
The topics I told you about
● I was bad at PowerShell, but now feel to fit it
● I found the way how to overcome to struggle
52. 52
The topics I told you about
● I was bad at PowerShell, but now feel to fit it
● I found the way how to overcome to struggle
● File specification and execution
● File operation
● Pipeline
53. 53
The topics I told you about
● I was bad at PowerShell, but now feel to fit it
● I found the way how to overcome to struggle
● File specification and execution
● File operation
● Pipeline
Learn You Your PowerShell
for Great Good!