Solved

Simpler powershell question

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

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…

730 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