Sei sulla pagina 1di 5

11/21/2016

ABeginner'sIntroductiontoWindowsPowerShell

ABeginnersIntroductiontoWindowsPowerShell

TOPUDEMYCOURSES:

MAY12,2014BYJENNIFERMARSH

TopJavaCourses
TopPythonCourses

WindowsPowerShellisanevolutionofthecommandlinea
combinationofaDOSshellandscriptingenvironment.Itcan

TopExcelCourses

helpwithrepetitivetasksprocessesactingonmanyfilesatonce
automatingandschedulingtasksandconfiguringWindows

LearnExcelWithThisGIFTutorial

componentsandservices.PowerShellisnotonlyforrunning
DOScommands,itisfarmoreflexiblethanthat.Itsupports
complexdecisionmaking,connectingtoawidevarietyofdata

BecomeaWebDeveloperfromScratch!(8100+
students)

sources,andevenbuildinggraphicaluserinterfaces.

AdvancedExcelTraining(42,660+students)
PowerShellisnowanessentialskillforITandserver
administrators,andisoftenusedwhendeploying

CodingforEntrepreneurs(4810+students)

maintenancescriptsacrossanentireorganization.Gettingthosescriptstorunsafelyonallmachinescanbeatechnical
process,buttipsandhelpareavailableforPowerShellusersworkingwithEnterpriseDesktopmachines.

iOSDevelopmentCodeCamp(1155+students)

Despitethefocusonadministrativetasks,homeusersandsoftwaredeveloperscanoftensavetimebymakinguseofthe
facilitiesavailable.

AdvancedJavaProgramming(735+students)

DownloadingandRunningPowerShell
MicrosoftincludesPowerShellwithWindows7and8.ForusersofWindowsXPandVista,youcandownloaditfrom
thedownloadsareaoftheMicrosoftScriptingCenter.

POPULARPOSTS
HowtoBuildaniPhoneAppfromScratchforNon
TechnicalPeople:Yourquickanddirtyguide

Onceinstalled,therearevariouswaysofrunningPowerShell:
OnWindows8,fromtheStartScreen,begintypingpowershellandeitherpressEnteraftertypingthewhole

ExcelFormulas:10FormulasThatHelpedMe
KeepMyJob

word,orclickthesearchresultnamedWindowsPowerShell.
OnWindows8,fromthedesktop,rightclicktheStartmenuicon,clickRun,andthentypepowershellandpress

CodeWars:RubyvsPythonvsPHP[Infographic]

Enter.
OnallversionsofWindows,presstheWindowslogokey+R,thentypepowershellandpressEnter.

GettingHelp
PowerShellintroducesoverahundrednewcommands,andthatisalottolearnwhenyoufirststart.However,it

Top10ProgrammingLanguagestoLearnin2014
HowtoAddRingtonesToYouriPhone(Updated
foriOS7)
8BestPowerPointPresentations:HowToCreate
EngagingPresentations

includesacomprehensivehelpsystemthatcanhelpyoufindcommandstoaccomplishtasks,anditexplainshowtouse
thosecommands.

JavaInterviewQuestions:HowtocracktheTOP
15questions

Thecommandforgettinghelpisgethelpanditacceptsavarietyofdifferentarguments.Youcandisplayhelpfora
particularcommandbyspecifyingthecommandsnameaftergethelp.Forexample,todisplaythehelpfileentryforthe
gethelpcommand,type:

gethelpgethelp

AndthenpressEnter.

DrupalvsJoomlavsWordPress:CMSShowdown
[infographic]
MakinganApp:6ThingsYouShouldConsider
BeforeGettingStarted
10FrmulasdeExcelparaserMsProductivo

Gethelpalsoacceptsakeywordforsearchingthedocumentation,andreturnsalistofresultscontainingthatkeyword.
Thiscanbeausefulwayoffindingthenamesofcommandsrelatedtospecifictopics.Tosearchforcommandsandhelp
filesrelatingtoWindowsservices,youcanrun:

gethelpservices

Helpfileentriesaredisplayeddirectlyintheconsole.However,byusingtheparameteronlineyoucanopenaweb
browserandviewthehelponMicrosoftsTechNetwebsite:

gethelpstartserviceonline

UsingtheConsole
https://blog.udemy.com/powershelltutorial/

1/5

11/21/2016

ABeginner'sIntroductiontoWindowsPowerShell

TheconsolewillbefamiliarinappearancetoanyonewhohasusedMSDOS,aDOSshell,orcommandlineonany
otheroperatingsystem.InPowerShell,specialcommands(knownascmdlets)aretypedinaftertheprompt,andare
executedwhenyoupresstheEnterkey.
Navigatingthroughthelocalfilesystemisthesameaswhenworkingintheearliershells,andyoucanstillusemanyof
theolderDOScommands.Forexample:

Executethenewcmdletsthesamewaytypetheirname,anyadditionalarguments,andthenpressEnter.Allcmdlets
followastandardnamingpatternofverbnoun,andnewPowerShellspecificcmdletsexistforalloftheearlierDOS
commands,althoughyouarenotrequiredtousethem.
Cmdletsappeartodisplaytheirresultsintheconsole,however,mostactuallyreturnanobjectthatisprocessedbythe
consoleandthendisplayed.Theydonotwritetothescreendirectly.ThisisapowerfulconceptandiswherePowerShell
haschangedsignificantlyfromearliershells.Insteadofsendingobjectstotheconsole,itispossibletosendobjectsto
othercmdletswithatechniqueknownaspiping.
Forexample,usethecmdletgetitempropertywiththenameofafiletodisplayitsbasicattributes:

getitempropertyc:\windows\regedit.exe

Theresultofthiscommandisactuallytoreturnanobjecttotheconsole,andthisobjectcontainsmanymorepiecesof
informationthanthedefaultviewshows.Bypipingtheobjecttoformatlist,otherpiecesofinformationaredisplayed:

getitempropertyc:\windows\regedit.exe|formatlist

Thepipesymbol(|)isusedtoseparatethetwocmdlets,andindicatesthattheoutputofthefirstcommandissentasthe
inputtothesecondcommand.
Whentheresultofacmdletconsistsofacollectionofobjects,pipingwilloftenprocesseachoneindividually.For
example:

dir|getitemproperty|formatlist

Theresultofdirisacollectionoffilesanddirectories,whicharepipedonebyoneintothecmdletgetitemproperty.The
itemspropertiesarethenpipedtoformatlisttodisplaythedetailedfileinformation.
Theexampleaboveshowsthreecommandsconnectedbypipes,butmorecomplicatedtaskscanuseasmanypipesas
needed.Thecodebelowtakeseachpieceofdetailedinformationandpipesittotheoutfilecmdlettowritethe
informationtoafile,insteadofreturningthetexttotheconsoleforviewingonscreen.

dir|getitemproperty|formatlist|outfileinfo.txt

ScriptinginPowerShell
Scriptsaretextfilesthatcontainsequencesofcallstocmdlets,andthesefileshavetheextension.ps1.However,unlike
workingintheconsole,eachoperationdoesnothavetobetypedinandexecutedimmediately.ThisallowsPowerShell
topreprocessthefileandextendthefacilitiestoincludethosecommonlyassociatedwithwritingsoftware,suchasuse
ofvariables,conditionalstatements,anduserdefinedfunctions.
Beforeyoucanwriteandrunscripts,youshouldbeawarethatWindowsisnotconfiguredtoallowtheexecutionof
unsignedscriptsbecausetheycanbeusedtodamagethesystem.Anindepthdiscussionofsecuritymodelsandcode

https://blog.udemy.com/powershelltutorial/

2/5

11/21/2016

ABeginner'sIntroductiontoWindowsPowerShell

signingisbeyondthescopeofthisarticle,buttoconfigureyourexecutionpolicysothatyoucanrunscriptsyouhave
written,butnotonesdownloadedfromtheInternet,youcanusethecommandsetexecutionpolicy:

setexecutionpolicyremotesigned

Asademonstration,thefollowingscriptusesthesamecmdletsastheexampleearliertooutputthedetailedinformation
foreveryfileordirectoryinthecurrentdirectory:

$list=dir
getitemproperty$list|formatlist|outfileout.txt

Wherethisscriptdiffersfromtheearlierconsoleoperationisthattheresultofdir(acollectionoffileanddirectory
references)isnotpipeddirectlyintogetitempropertyitisfirststoredinavariablenamedlist.Variablesaredeclared
andusedinPowerShellbyplacingadollarsigninfrontoftheirname.
ThattwolinescriptcanbecreatedinNotepad,thensavedinthecurrentdirectorywiththefilenametest.ps1.Youcan
thenrunthescriptbytypingitspathandfilenameintheconsoleandpressingEnter.
.\test.ps1
Youcanrerunthescriptatanytime,savingyoufromhavingtotypeallthestatementsagainandasthescriptfileis
portable,youcanrunthesamescriptondifferentmachines.
Scriptingishighlyflexible,asshownbelow:

$list=dir
foreach($itemin$list){
$fn=$item.name+"_.txt"
getitemproperty$item|formatlist|outfile$fn
}

Asbefore,thedirectorylistingisstoredinthevariablelist.However,thistimeeachitemisprocessedindividuallybythe
scriptandnotbythecmdlet.
Theforeachlooprunsthrougheveryiteminthelistandtheinstructionsbetweencurlybracesarerunforeachone:
1.Thecurrentitemfromlistisassignedtothevariableitem.Eachitemisanobjectdescribingthepropertiesofa
fileordirectory.
2.Anewvariablefniscreatedthisisavaluecalculatedbytakingthename(filename)propertyoftheitem,and
addinganunderscoreand.txt.
3.Thedetailedpropertiesforeachitemarethenretrievedbypassingthecurrentitemasanargumenttoget
itemproperty.
4.Thereturnedobjectispipedintoformatlisttogetthedetailedview.
5.Finally,thepropertiesoftheitemarewrittentoanewtextfileusingthefilenamethatwascalculatedonthe
precedingline.
Ifyourunthatsamplecodeinadirectorywithalotofcontents,alargenumberofnewtextfileswillbecreated.Youcan
removethembywritinganotherPowerShellscript:

$list=dir
foreach($itemin$list){
$fn=$item.name+"_.txt"
if(testpath$fn){
del$fn
}
}

Theexampleaboveprocessesthedirectorylistinginasimilarwaytothepreviousscript.Thistime,however,the
conditionalstatementifandthecmdlettestpathareusedtocheckifthefileexistsandifthatistrue,thescriptdeletes
thefile.

FurtherReading
https://blog.udemy.com/powershelltutorial/

3/5

11/21/2016

ABeginner'sIntroductiontoWindowsPowerShell

AsthisisonlyashortintroductiontoPowerShell,itisnotpossibletoshowalloftheavailablecmdletsorfeaturesofthe
scriptinglanguage.AndthereisnotspacetoshowhowPowerShellcanbeusedtocommunicatewithWindows
processesandservices,whichisanimportantpartofitsuseforITadministrators.PowerShellfortheEnterprise
DesktopandPowerShell:AGettingStartedGuideforITAdminsofferafarmoredetailedintroduction,
includingtheconceptsandcommandsthathavenotbeendescribedabove.
PowerShellisbasedonMicrosofts.NETframework,andsodeveloperscanwritetheirowncmdletsusingMicrosoft
VisualStudioandthelanguagessupportedbythecommonlanguageruntime(CLR),suchasC#.Cmdletsareintendedto
replaceconsoleapplications,andcompletebeginnerscanlearnC#programmingtobeginwritingtheirown,in
muchthesamewayastheywouldwriteacommandlineprogram.

FiledUnder:Students,Technology

Returntotopofpage

https://blog.udemy.com/powershelltutorial/

Copyright2016Udemy,Inc.BuiltontheGenesisFramework

4/5

11/21/2016

https://blog.udemy.com/powershelltutorial/

ABeginner'sIntroductiontoWindowsPowerShell

5/5

Potrebbero piacerti anche