Solved

Simpler powershell question

Posted on 2011-02-14
4
616 Views
Last Modified: 2012-05-11
Im playing with powershell in order to learn more about it. So probably a very simple question here.

I have a directory called c:\test in it there is a single text file. Now if i run the following command

dir | get-member

It returns all the methods and properties of the text file. Some of these i see are creationtime, lastaccessedtime, fullname, etc.

What I would like to know is how do i actually return those values? So say I wanted to get the lastaccessedtime of the file that is in my test directory. How would I go about that.


0
Comment
Question by:Joseph Daly
4 Comments
 
LVL 27

Assisted Solution

by:KenMcF
KenMcF earned 166 total points
ID: 34891330
you would do something like this

gci c:\NAMEOFFILE.txt | Select name, lastwritetime

if you want to export that

gci c:\NAMEOFFILE.txt | Select name, lastwritetime
 | export-csv c:\export.csv
0
 
LVL 35

Author Comment

by:Joseph Daly
ID: 34891401
Is there another way of doing this other than GCI? I watched a webex session today where the person used another method of getting the data not using GCI. Unfortunately I was trying to multitask during the presentation and figured I could watch the replay, but they do not have it available for replay.

From what I remember it had something to do with using a . (dot) seperator to get the info. I think it had to do with the get-member cmdlet????
0
 

Assisted Solution

by:JOSHUABT
JOSHUABT earned 167 total points
ID: 34894203
I think what you might be looking for is something like this:

$d = gci .\
$d[0].lastwritetime

A collection of files is returned from the current folder.  You can then reference the first file by the objects subscript.  All of the properties can be accessed by using the dot notation.

You can determine how many files were returned to your collection type: $d.count

if yuu want to see the methods just type: $d | gm

--- Hope that helps.




0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 167 total points
ID: 34895404
Just in case:

GCI is an alias for Get-ChildItem. dir and ls are also aliases for get-ChildItem.

That means that each of these commands are identical:
Get-ChildItem c:\stuff | Select-Object Name, LastWritetime, LastAccessTime, CreationTime
gci c:\stuff | Select-Object Name, LastWritetime, LastAccessTime, CreationTime
dir c:\stuff | Select-Object Name, LastWritetime, LastAccessTime, CreationTime
ls c:\stuff | Select-Object Name, LastWritetime, LastAccessTime, CreationTime

Open in new window

You can see all of the aliases for a specific CmdLet like this:
Get-Alias -Definition Get-ChildItem

Open in new window

Or you can find out which command something is an alias for:
Get-Alias gci

Open in new window

You'll see a lot of this type of thing, although I endeavour to avoid aliases in my examples, clarity is best :)

Now that gets you so far, you can see that we can select specific properties from the object (the directory listing in this case). If we want to access a single property we can do this:
(Get-Item "SomeFile.txt").LastWriteTime

Open in new window

Or:
Get-Item "SomeFile.txt" | Select-Object -ExpandProperty LastWriteTime

Open in new window

You can also access properties on objects in the pipeline:
# Filtering results
Get-ChildItem | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-120) }

# Looping
Get-ChildItem | ForEach-Object {
  If ($_.LastWriteTime -lt (Get-Date).AddDays(-120)) {
    Write-Host "$($_.FullName) is really old"
  } Else {
    Write-Host "$($_.FullName) was last written on $($_.LastWriteTime)"
  }
}

Open in new window

In this example, $_ is the Pipeline object, it represents the current value in a loop. Where-Object is a bit of an implicit loop, it performs the comparison with a date 120 days ago for each value it gets from Get-ChildItem.

Chris
0

Featured Post

Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now