PowerShell学习笔记-2-129 cmdlets
上一篇 / 下一篇 2012-11-21 14:53:58 / 个人分类:PowerShell
- get-childitem
例二: get-childitem c:\restored\*.* -include *.doc,*.xls
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}
TAG:
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 36328
- 日志数: 49
- 图片数: 1
- 建立时间: 2007-04-18
- 更新时间: 2014-02-07