PowerShell学习笔记-2-129 cmdlets

上一篇 / 下一篇  2012-11-21 14:53:58 / 个人分类:PowerShell

学习内容

129个cmdlets学习

IntroductionToWindowsPowerShell
  • Exercise 1 - 
  • Listing Files
    1. get-childitem
    (1)可以使用逗号(,)分隔多个参数值。
    例一:Of course we can; this command tells Get-ChildItem to ignore both .tmp and .temp
    files:
    get-childitem c:\restored -exclude *.tmp,*.temp
    (2)使用参数 -include 时,需要注意,如果希望只查看*.doc 和 *.xls 类型的文件时,在文件夹后面必须加*.*。
    例二: get-childitem c:\restored\*.* -include *.doc,*.xls

    2. set-location
    等同于cd 
        (To get back to your home directory type the following, with the tilde (~) being
        shorthand for your home folder:
        cd ~)

        3. New-PSDrive : To map a Windows PowerShell drive to the folder

        Name                              Category  Synopsis
        ----                              --------  --------
        New-PSDrive                       Cmdlet    Creates a Windows PowerShell drive in the current session.
        例三:
        PS〉new-psdrive x -psprovider filesystem -root c:\restored
        之后,可以在任何文件夹下使用 x 代替 c:\restored, 比如:
        PS D:\> get-childitem x:
        结果会显示c:\restored 下面的文件列表。

        4. get-alias
        To view all the aliases that ship with Windows PowerShell type.

        Exercise 2 Sorting Data

         

        1.     1. Sort-Object

        可以和Get-Childitem 结合使用

        Example

        get-childitem c:\restored | select-object name, length | sort-object length

        get-childitem c:\restored | select-object name, length | sort-object length -descending | select-object -first 10

        2.     由使用上面的cmdlets产生的问题,我如何知道我之后使用的object有哪些属性呢?

        答案:2. Get-Member

        Example:

        get-childitem c:\restored | get-member

        get-childitem | get-member -membertype property


        Exercise 3 Grouping Items and Calculating Statistics

        Grouping Items by File Type

        Group-Object

        Example:

        get-childitem c:\restored | group-object extension

        get-childitem c:\restored | group-object extension | sort-object count

         

        get-childitem c:\restored | group-object {$_.CreationTime.Year},{$_.CreationTime.Month}

        其中,$_表示的是当前观察对象自己。

         

        Determining Overall File Size

        Measure-Object

        Example:

        PS X:\> get-childitem | measure-object length -average -sum -maximum -minimum

        (output result:

         

        Count    : 12

        Average  : 1783451.

        Sum      : 21401417

        Maximum  : 11647740

        Minimum  : 23621

        Property : length

         

        )

         

        Of course, you can also use wildcards to focus in on a specific set of files.

        Example:

        get-childitem c:\restored\*.tmp | measure-object length –sum

         

        Displaying a Single Property Value

        Note:Parentheses serve a similar function in Windows PowerShell. Let’s take another

        look at our command:

        Example:

        PS X:\> (get-childitem X:\*.txt| measure-object length -sum).sum

        Output result:

        1521500


        Exercise 4 Deleting Files

        Deleting All Files with a Specified File Extension

        Remove-Item

        Example:

        remove-item c:\restored\*.tmp

        To verify that all the .tmp files have been deleted from C:\Restored type this

        command into the command window and then press ENTER:

        get-childitem c:\restored\*.tmp

        Deleting All Files Larger than a Specified Size

        Example:

        Note:Let’s take a look at the command that deletes all the files larger than 1

        megabyte from the folder C:\Restored

        PS>get-childitem c:\restored | where-object {$_.length –gt 1048576 } | foreach-object {remove-item $_.fullname}

        (PS:$_ representing the individual items in the collection, andFullNamebeing the file property that contains thecomplete path.)

         

        In Windows PowerShell you will typically use the following comparison operators:

        Operator Definition

        -lt Less than

        -le Less than or equal to

        -gt Greater than

        -ge Greater than or equal to

        -eq Equal to

        -ne Not equal to

        -contains Determine elements in a group. This always returns

        Boolean $True or $False.

        -notcontains Determine excluded elements in a group. This always

        returns Boolean $True or $False.

        -like Like - uses wildcards for pattern matching

        -notlike Not Like - uses wildcards for pattern matching

        -match Match - uses regular expressions for pattern matching

        -notmatch Not Match - uses regular expressions for pattern

        matching

         

        Using File Size Designators

        If you want to work with kilobytes, megabytes, and

        gigabytes all you have to do is use the appropriate designator: KB, MB, or GB.

        Example:

        get-childitem c:\restored | where-object {$_.length –gt 1MB} | foreach-object {remove-item $_.fullname}


        Exercise 5 Creating Folders

        Create a New Folder

        New-Item

        Example:

        new-item c:\restored\test -type directory

        Verifying the Existence of a Folder

        Test-Path

        Example 1:

        test-path c:\restored\test

        Example 2:

        Here’s a cool thing you can do with Test-Path. Want to know if there are any .xls

        files in the folder C:\Restored? Type this command into the command window,

        pressENTER, and see for yourself:

        test-path c:\restored\*.xls

        Auto-Create New Folders Based on File Extension

        Example:

        get-childitem c:\restored | select-object extension | sort-object extension –unique | foreach-object {new-item ("c:\restored\restored_files" + $_.Extension) -type directory}

        这个例子有点复杂,需要看原始文档,这里就不解释了。

        Exercise 6 Moving Files to Different Folders

        Moving Files Based on File Extension

        Move-item

        a single command to move all the files in the folder C:\Restored to a new home. Let’s take a look at the command, and then explain how it works:

        Example:

        get-childitem c:\restored | where-object {$_.mode -notmatch "d"} | foreach-object {$b ="c:\restored\restored_files" + $_.extension; move-item $_.fullname $b}

        Exercise 7 Viewing All the Files in a Folder and Its Subfolders

        Listing All the Files in a Folder and Its Subfolders

        To view the list of folders and subfolders only, type the following command into

        the command window and then pressENTER:

         

        get-childitem c:\restored -recurse | where-object {$_.mode -match "d"}

        Exercise 8 Saving Data to a Text File

        Outputting Data to a Text File

        Windows PowerShell provides a number of ways to write data to a text file, including

        the CmdletsAdd-ContentandSet-Content. For today, however, we’re going to take

        the easiest possible route and simply use the redirection character>to redirect our

        output to a text file.

        Example:

        get-childitem c:\restored -recurse > c:\restored\output.txt

         

        Get-Content

        Example:

        However, you can verify that the output was saved by typing the following

        command into the command window and pressingENTER:

        get-content c:\restored\output.txt

         

        You canappenddata to a file by using the>>redirection characters. New part will be displayed after old part.

         

        Out-File

        get-childitem c:\restored -recurse | out-file c:\restored\test.txt

         

        Outputting Data to a Text File and Displaying it On Screen

         

        Tee-Object

        Tee-ObjectCmdlet, which can simultaneously output data to two

        places, such as a text file and the screen.

        Exercise 9 Working with Properties and Methods

        Modifying Property Values

        Example:

        get-childitem c:\restored -recurse | foreach-object {$b=get-date; $_.lastwritetime = $b}



        Continuing...

        TAG:

         

        评分:0

        我来说两句

        Open Toolbar