Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simpler powershell question

Posted on 2011-02-14
4
Medium Priority
?
629 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
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…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

610 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