?
Solved

Simpler powershell question

Posted on 2011-02-14
4
Medium Priority
?
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Assisted Solution

by:KenMcF
KenMcF earned 664 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 668 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 71

Accepted Solution

by:
Chris Dent earned 668 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
Suggested Courses

765 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