Many people wants to know what is powershell ?.
Is it a more powerful cmd ?
Is it a powerful environment ?
If I have Windows 2003, can I use powershell or it is only for Windows 2008 ?
If you have worked with UNIX and Linux, powershell is a natural need that Microsoft required.
Visual Basic was not as powerfull as the born shell, c shell to interact with the OS.
Microsoft noticed, that it was necessary a more powerful shell to support variables, loops, functions, etc.
Actualmente el cmd era muy limitado para la programación. El Visual Script surgió como una alternativa para automatizar tareas, pero es relativamente lento y se necesitan muchas líneas de código para tareas sencillas que en otros lenguajes como PERL se harían mas fácilmente.
Getting started
Let start with the hello world:
PS C:\> echo ¨Hello world¨
Use variables in powershell
PS C:\>$var=hola mundo
PS C:\>echo $var
How to get help
There are 3 ways to do it:
PS C:\>get-help dir
PS C:\>man dir
PS C:\>help dir
How to get the list of all the commands in powershell
PS C:\>get-help *
More examples about commands in powershell:
http://technet.microsoft.com/en-us/library/dd347701.aspx
Show the drives in Powershell
Get-PSDrive.
This command let you show the different drives.
Use aliasThe alias let you rename or create your own names for the commands.
PS C:\> set-alias get-drive get-psdrive
This command create an alias to the get-psdrive. The new name created for the command is get-drive.
If you execute it, you will have the same results than the get-psdrive
Finalmente, salvamos el archivo y cerramos y abrimos el powershell
Registry
In order to go to the registry in powerhsell use this:
PS C:\> cd hklm:
In order to return to the c: drive use this command:
PS C:\> c:
To clean the screenshot use cls:
PS C:\> cls
If you need to go to the registry drive again use the cd.
PS C:\> cd hklm:
If you want to go to a specific path:
PS HKLM:\> cd software\clients\Calendar\Microsoft*\
Protocols
The command used to create a new registry with powershell is the following:
PS HKLM:\software\clients\Calendar\Microsoft Outlook\Protocols>mkdir reg_nuevo
In order to return to the root use this commad:
PS HKLM:\software\clients\Calendar\Microsoft Outlook\Protocols>cd /
To list the children of the HKLM folder:
PS HKLM:\>Get-ChildItem
PS HKLM:\>ls
PS HKLM:\>gci
PS HKLM:\>dir
If you want to return to the c drive:
PS HKLM:\> c:
Process
In order to display the list of processes
PS C:\> Get-Process
In order to stop a process
PS C:\> Stop-Process -processname [process]
If you need to list the services
PS C:\>Get-service
In order to start a service use this commad:
PS C:\>start-service [service_name]
In order to stop a service use this commad:
PS C:\>stop-service [service_name]
Powershell scripts
By default the scripts are disabled. You can try to execute with this command:
PS C:\> ./prueba.ps1
If you need to enable the policies to run scripts, use this command:
PS C:\> set-executionpolicy unrestricted
Once enable, try again
PS C:\> ./prueba.ps1
How to create a batch file to call a ps script
Step 1.
Create a ps1 file.
Filename: creardir.ps1
Content: mkdir Daniel
Step 2. Create a file with the .bat extension
Filename: createdir.bat
Content: powershell.exe -command .\creardir.ps1
In order to create an event in the event viewer:
PS C:\Documents and Settings\user> EVENTCREATE /T ERROR /ID 1000 /L APPLICATION /D "Test Error"
In order to see the event in the application log:
PS C:\Documents and Settings\user> Get-EventLog -logName "Application"