SlideShare a Scribd company logo
1 of 53
Download to read offline
1
One man loves PowerShell
once he failed
@gab_km
12th July, 2014
Japan PowerShell meetup #3
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
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
4
Once upon a time...
5
Once upon a time...
In this tweet, I have started PowerShell 2
years ago from this time(24/Mar/2011).
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.
7
In these days...
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
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
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
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.
12
Case 2
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
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.
15
Case 3
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
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
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.
19
● File specification and execution
● File operation
● Pipeline
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.
21
PS> notitle.ps1
notitle.ps1 : 用語 'notitle.ps1' は、コマンドレット、関数、スクリ
プト ファイル、または操作可能なプ
ログラムの名前として認識されません。名前が正しく記述されていることを確認
し、パスが含まれている場合
はそのパスが正しいことを確認してから、再試行してください。
発生場所 行 :1 文字 :1
+ notitle.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound:
(notitle.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS>
File specification and execution
22
PS> notitle.ps1
notitle.ps1 : 用語 'notitle.ps1' は、コマンドレット、関数、スクリ
プト ファイル、または操作可能なプ
ログラムの名前として認識されません。名前が正しく記述されていることを確認
し、パスが含まれている場合
はそのパスが正しいことを確認してから、再試行してください。
発生場所 行 :1 文字 :1
+ notitle.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound:
(notitle.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS>
You will see this error message when you simply
replace from .bat commands to .ps1 commands.
File specification and execution
23
To specify files(1)- Absolute path
PS> C:myposhnotitle.ps1
24
To specify files(1)- Absolute path
PS> C:myposhnotitle.ps1
Hello, there!
PS>
This is a foolproof and verbose method.
25
To specify files(2)- Use '.'
PS> .notitle.ps1
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
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
PS> $myScript = “C:myposhnotitle.ps1”
PS> $myScript
C:myposhnotitle.ps1
PS>
Case - You cannot execute a file
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
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
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
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.( ◜◡◝ )
33
● File specification and execution
● File operation
● Pipeline
34
File operation
● Copy
● Rename
● Delete
There were demands for files to:
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
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
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
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
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
Delete files
PS> ls | % { $_.Name }
notitle.ps1
PS> Remove-Item .notitle.ps1
PS> ls | % { $_.Name }
PS>
Use Remove-Item to delete files.
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.
42
● File specification and execution
● File operation
● Pipeline
43
Pipeline
There is a strong relationship like this:
“Associate PowerShell with pipeline,
44
Pipeline
There is a strong relationship like this:
“Associate PowerShell with pipeline,
associate pipeline with F# PowerShell”
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
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
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
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
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
Pipeline - serial processing(2)
@(1 .. 3) | % { $_ + 2 }
# 3
# 4
# 5
There is an useful alias for Foreach-Object:
'%' symbol.
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
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
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!

More Related Content

What's hot

Unix Basics
Unix BasicsUnix Basics
Unix Basics
Dr.Ravi
 
Unit 8
Unit 8Unit 8
Unit 8
siddr
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 

What's hot (20)

Linux final exam
Linux final examLinux final exam
Linux final exam
 
7 Common mistakes in Go and when to avoid them
7 Common mistakes in Go and when to avoid them7 Common mistakes in Go and when to avoid them
7 Common mistakes in Go and when to avoid them
 
Unix 2 en
Unix 2 enUnix 2 en
Unix 2 en
 
Mission ImpAnsible - NSM at (RobotFrame)work
Mission ImpAnsible - NSM at (RobotFrame)work Mission ImpAnsible - NSM at (RobotFrame)work
Mission ImpAnsible - NSM at (RobotFrame)work
 
Unix Basics
Unix BasicsUnix Basics
Unix Basics
 
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
Go for Object Oriented Programmers or Object Oriented Programming without Obj...Go for Object Oriented Programmers or Object Oriented Programming without Obj...
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
 
Unit 8
Unit 8Unit 8
Unit 8
 
Batch conv3.2 1
Batch conv3.2 1Batch conv3.2 1
Batch conv3.2 1
 
Infrastructure as code might be literally impossible
Infrastructure as code might be literally impossibleInfrastructure as code might be literally impossible
Infrastructure as code might be literally impossible
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
Getting Started with Go
Getting Started with GoGetting Started with Go
Getting Started with Go
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
 
Wait what? Postgresql can do that?
Wait what? Postgresql can do that?Wait what? Postgresql can do that?
Wait what? Postgresql can do that?
 
Lpt lopsa
Lpt lopsaLpt lopsa
Lpt lopsa
 
Shell script-sec
Shell script-secShell script-sec
Shell script-sec
 
3.3. Database honeypot
3.3. Database honeypot3.3. Database honeypot
3.3. Database honeypot
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 

Similar to One man loves powershell once he failed

cs3157-summer06-lab1
cs3157-summer06-lab1cs3157-summer06-lab1
cs3157-summer06-lab1
tutorialsruby
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
PowerShell Core Skills (TechMentor Fall 2011)
PowerShell Core Skills (TechMentor Fall 2011)PowerShell Core Skills (TechMentor Fall 2011)
PowerShell Core Skills (TechMentor Fall 2011)
Concentrated Technology
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
Matt Harrison
 

Similar to One man loves powershell once he failed (20)

cs3157-summer06-lab1
cs3157-summer06-lab1cs3157-summer06-lab1
cs3157-summer06-lab1
 
Puppi. Puppet strings to the shell
Puppi. Puppet strings to the shellPuppi. Puppet strings to the shell
Puppi. Puppet strings to the shell
 
Why and How Powershell will rule the Command Line - Barcamp LA 4
Why and How Powershell will rule the Command Line - Barcamp LA 4Why and How Powershell will rule the Command Line - Barcamp LA 4
Why and How Powershell will rule the Command Line - Barcamp LA 4
 
Document Library Folder Operations User Guide
Document Library Folder Operations User GuideDocument Library Folder Operations User Guide
Document Library Folder Operations User Guide
 
Puppet quick start guide
Puppet quick start guidePuppet quick start guide
Puppet quick start guide
 
FireWorks workflow software
FireWorks workflow softwareFireWorks workflow software
FireWorks workflow software
 
Get-Help: An intro to PowerShell and how to Use it for Evil
Get-Help: An intro to PowerShell and how to Use it for EvilGet-Help: An intro to PowerShell and how to Use it for Evil
Get-Help: An intro to PowerShell and how to Use it for Evil
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
PowerShell Core Skills (TechMentor Fall 2011)
PowerShell Core Skills (TechMentor Fall 2011)PowerShell Core Skills (TechMentor Fall 2011)
PowerShell Core Skills (TechMentor Fall 2011)
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdfLearn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
 
Python1
Python1Python1
Python1
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Perl_Part3
Perl_Part3Perl_Part3
Perl_Part3
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShell
 
Powershell notes
Powershell notesPowershell notes
Powershell notes
 
Lean Php Presentation
Lean Php PresentationLean Php Presentation
Lean Php Presentation
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

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
  • 4. 4 Once upon a time...
  • 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.
  • 19. 19 ● File specification and execution ● File operation ● Pipeline
  • 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.
  • 21. 21 PS> notitle.ps1 notitle.ps1 : 用語 'notitle.ps1' は、コマンドレット、関数、スクリ プト ファイル、または操作可能なプ ログラムの名前として認識されません。名前が正しく記述されていることを確認 し、パスが含まれている場合 はそのパスが正しいことを確認してから、再試行してください。 発生場所 行 :1 文字 :1 + notitle.ps1 + ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (notitle.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS> File specification and execution
  • 22. 22 PS> notitle.ps1 notitle.ps1 : 用語 'notitle.ps1' は、コマンドレット、関数、スクリ プト ファイル、または操作可能なプ ログラムの名前として認識されません。名前が正しく記述されていることを確認 し、パスが含まれている場合 はそのパスが正しいことを確認してから、再試行してください。 発生場所 行 :1 文字 :1 + notitle.ps1 + ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (notitle.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS> You will see this error message when you simply replace from .bat commands to .ps1 commands. File specification and execution
  • 23. 23 To specify files(1)- Absolute path PS> C:myposhnotitle.ps1
  • 24. 24 To specify files(1)- Absolute path PS> C:myposhnotitle.ps1 Hello, there! PS> This is a foolproof and verbose method.
  • 25. 25 To specify files(2)- Use '.' PS> .notitle.ps1
  • 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.( ◜◡◝ )
  • 33. 33 ● File specification and execution ● File operation ● Pipeline
  • 34. 34 File operation ● Copy ● Rename ● Delete There were demands for files to:
  • 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.
  • 42. 42 ● File specification and execution ● File operation ● Pipeline
  • 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!