Solved

Simpler powershell question

Posted on 2011-02-14
4
613 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

759 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

22 Experts available now in Live!

Get 1:1 Help Now