Showing posts with label Get-Alias. Show all posts
Showing posts with label Get-Alias. Show all posts

Friday, March 2, 2018

Working with Files and Folders using PowerShell

In this article I’ll explain various PowerShell commands to perform File/Directory operations.

Get-ChildItem

You can use ‘Get-ChildItem’ command to get list of all the files and folder of given path.

Get-ChildItem -Path C:\PowerShell 

You can use below commands to list either files or folders only of given path

Get-ChildItem -Path C:\PowerShell -File

Get-ChildItem -Path C:\PowerShell -Directory

You can use below command to filter files and folders as per your requirement.

Get-ChildItem -Path C:\PowerShell -Filter *.pdf



You can you below command to list all the files and folders of give path recursively.

Get-ChildItem -Path C:\PowerShell -Recurse




You can also use Include or Exclude parameters with Get-ChildItem command to filter files.

Get-ChildItem -Path C:\PowerShell -Include "*.pdf" -Exclude "s*.PDF" –Recurse


New-Item
You can use ‘New-Item’ command to create new file or directory.

New-Item -Type File "C:\PowerShell\First1.txt"

New-Item -Type Directory "C:\PowerShell\MyFolder"




Copy-Item

You can use Copy-Item command to copy files and directories.

Copy-Item First1.txt Mitesh\First2.txt
Get-ChildItem -Path C:\PowerShell\Mitesh


Remove-Item

You can use Remove-Item command to delete files and directories.

Remove-Item c:\PowerShell\Mitesh\First2.txt
Get-ChildItem -Path C:\PowerShell\Mitesh


Rename-Item

You can use ‘Rename-Item’ command to rename file and directories.

Rename-Item First1.txt First3.txt
Get-ChildItem -Path C:\PowerShell\Mitesh




Move-Item
You can use ‘Move-Item’ command to move files and directories.

Move-Item c:\PowerShell\First3.txt C:\PowerShell\Mitesh\First1.txt
Get-ChildItem C:\PowerShell\Mitesh



Summary

Alias
Cmdlet
Description
dir
Get-ChildItem
List files and folders
ni
New-Item
Create new files and folders
copy
Copy-Item
Copy files and folders
del/rmdir
Remove-Item
Delete files and folders
ren
Rename-Item
Rename files and folders
move
Move-Item
Move files and folders


I hope you now have some basic understanding about PowerShell file and folder cmdlets to play around it. 

Thank you for reading this article. Please leave your feedback in comments below.

Reference –

See Also –

Saturday, February 24, 2018

Introduction to Windows PowerShell


As name suggest, Windows PowerShell is powerful command line shell. Windows PowerShell is built on top of .Net framework and internally uses .Net framework objects to run commands. PowerShell uses cmdlets (“command-let”) to perform various actions. PowerShell ships with hundreds of cmdlets and you can also create your own cmdlets as per your need. Windows PowerShell is installed by default with Windows 7 SP1 onwards editions.

PowerShell is a tool and can be used to perform day to day activities. This tool is very useful to developers and administrators to do their activities and they can write scripts or programs to ease their work. Like many other shells, Windows PowerShell allows you to access file/directory system and other computer/network/remoting related information.

In this article, I’ll explain how to start PowerShell and introduce some basic commands for you to start with PowerShell.


How to start PowerShell


For windows 10 – search PowerShell on search bar. (Pre-installed with windows 10)

Once you launch PowerShell below window will appear.


You can use directory commands like DIR, CD, CD\, CD.., MKDIR, RMDIR, COPY, DEL etc with PowerShell that you already using with Command Prompt.

When you execute these commands on PowerShell, internally PowerShell uses other cmdlets. For example, when you type DIR command on PowerShell window, it will internally use Get-ChildItem command to list all directories and files.

Get-Alias


You can use Get-Alias command to check which underlying command is mapped with alias.

Get-Alias dir

To check the entire alias you can type Get-Alias command without parameter. 

Get-Alias

Get-Command


You can use ‘Get-Command’ to check what all commands available in PowerShell. You can use ‘|more’ to view details in page wise manner.

Get-Command | more



You can also search available command using wildcards like below.

Get-Command *process

You can also use verb or noun parameters to get all the commands with specified verb or noun

Get-Command -Verb get | more

Get-Command -noun process

You can use below command to check total commands currently available in PowerShell.

(Get-Command).Count

Get-Help

You can use Get-Help command to get help about any commands in PowerShell.

Get-Help Get-ChildItem | more

If you would like to know help only about specific parameter of that command then you can use below command.

Get-Help Get-ChildItem -Parameter filter

You can get help of any command in all the details with example using below command.

Get-Help Get-ChildItem -Full |more

If your PowerShell help is not updated since long time then you can use below command to update help. This command will take some time to execute and download help.

Update-Help -Force

I hope you now have some basic knowledge about how to start PowerShell and how to search various commands and get help about them. 

Thank you for reading this article. Please leave your feedback in comments below.

Reference –